Convert "HH:MM AM/PM" varchar column to time in Teradata - sql

I need to convert the following varchar column to a 24 hr database time format in Teradata.
1:00 AM
6:45 PM
10:15 AM
9:30 PM
9:45 AM
8:30 PM
1:15 PM
Any pointers to existing solutions will be very helpful. I have not been able to find it.

Add the leading zero if missing and then cast using a format:
cast(lpad(col,8,'0') as time(0) format 'HH:MIBT')

Related

Converting a date

I am using Selenium and using xpaths and I am trying to convert a date. I do have the DateTime library.
I need the date to be 1/15/23 but every time it logs it to console as 01/15/23. I am not sure why it logs it as 01 and not 1 on the month.
This is what I am using:
result_format=%m/%d/%y 12:00 AM date_format=%m/%d/%Y
It wouldn't b a big deal but I am using that date to compare against another page and it fails because of the 01.
Thanks for any help!
result_format=%m/%d/%y 12:00 AM date_format=%m/%d/%Y
Expecting it to log like this: 1/15/23 12:00 AM
I have used the datetime library and converted datetime to str and removed 0 from prefix.
import datetime
time1 = datetime.datetime(2020, 5, 17).strftime("%m/%d/%y 12:00 AM").removeprefix("0")
print(f"time1: {time1}")
time2 = datetime.datetime(2020, 12, 17).strftime("%m/%d/%y 12:00 AM").removeprefix("0")
print(f"time2: {time2}")
Output
time1: 5/17/20 12:00 AM
time2: 12/17/20 12:00 AM
Hope this helps. Happy Coding :)

Not converting Tick to Ohlc 1hr data convert problem

Tick to Ohlc 1hr data always start from 00:00:00 like 9:00:00 to 10:00:00. Even my data start from 9:15 it's not convert from 9:15 to 10:15....it just convert from 9:00:00 to 10:000
Solve this 1hr convert

Convert String column which has AM/PM to Timestamp in Impala

I have a column which has values in string type like below:
31-Oct-2016 12:00 AM
31-May-2015 12:00 PM
I want to convert the above column values to timestamp in IMPALA. Tried with cast, to_timestamp and other ways , but it is either showing syntax error or Null as result.
Can you please suggest a solution
2nd Requirement
There is a column like below in string, I want it to be converted to timestamp alone.
31-Oct-2016 12:00
31-May-2015 12:00
please suggest a way, I'm new to Impala
Thanks in advance
You can use below code. Unfortunately, impala doesn't have am pm conversion capability, but you can use a little code to identify PM and add 12 hours to that to convert properly.
select
if (right('31-Oct-2016 02:09 PM',2)='PM',
to_timestamp('31-Oct-2016 02:09 PM','d-MMM-yyyy H:m') + interval 12 hours,
to_timestamp('31-Oct-2016 02:09 PM','d-MMM-yyyy H:m')
) ampm_timestamp
Second requirement -
Impala always expects 24hour hour format when it converts datetime. So, in your case for 12 AM scenario, we have to do some special logic like below.
First check if its 12 AM, then minus 12 hours, else check if its PM, then add 12 hours(which covers 12PM scenario) and finally if its any other AM, it simply converts to timestamp.
select
CASE WHEN right('31-Oct-2016 12:09 AM',2)='AM' AND RIGHT( SPLIT_PART('31-Oct-2016 12:09 AM',':',1),2)='12'
THEN to_timestamp('31-Oct-2016 12:09 AM','d-MMM-yyyy HH:mm') - interval 12 HOURS
ELSE CASE WHEN right('31-Oct-2016 12:09 AM',2)='PM'
THEN to_timestamp('31-Oct-2016 12:09 AM','d-MMM-yyyy HH:mm') + interval 12 HOURS
ELSE to_timestamp('31-Oct-2016 12:09 AM','d-MMM-yyyy HH:mm')
END END AMPM_TIMESTAMP

How to cast 12/01/2019 12:00:00 a.m. to DATETIME in BigQuery

I'm trying to convert a String (12/01/2019 12:00:00 a.m.) to DATETIME.
I have tried with:
PARSE_DATE('%e/%m/%Y %k:%M:%S %P', Fecha_Desc)
no result ... any help?
Two things. First, you need parse_datetime(). Second a.m. is not recognized. So, remove the spaces:
SELECT PARSE_DATETIME('%e/%m/%Y %k:%M:%S %p', replace('12/01/2019 12:00:00 a.m.', '.', ''))
If you really want a date, convert to a date after converting to a datetime -- or just convert the first 10 characters.

postgresql - Convert string to time

I have column where I saved the transaction time, format is HHMMSS
for example:
140159
013115
235900
then I want to convert those time to HH:MM AM/PM
so results would be:
2:01 PM
1:31 AM
11:59 PM
Here are the queries ive tried, but none of them return the results I want..
SELECT TO_CHAR(TO_TIMESTAMP(TRANSTIME,'hh24:mi:ss AM'),'hh12:mi:ss AM')
FROM PRODUCTSALES order by TRANSTIME desc LIMIT 100
SELECT TO_TIMESTAMP(TRANSTIME, 'HH24:MI')::TIME
time data type is just time - not a format. to get time with wanted format use to_char, eg fro your 140159:
t=# select to_char('140159'::time,'HH:MI AM');
to_char
----------
02:01 PM
(1 row)
Mind I first cast as time and only then format it