postgresql - Convert string to time - sql

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

Related

How to convert VARCHAR (AM/PM) to TIMESTAMP (24 h) in SQL (Teradata v17)

I've tried multiple solutions, but I keep getting errors. I need to create a new column casting VARCHAR to TIMESTAMP that includes AM, PM or -ideally- changes it to 24 hrs format.
VARCHAR format (Start_Date column): 8/3/2022 4:58:49 PM
I found the below solution is some other post, but I'm getting error: 'Format code appears twice'
SELECT itab.*,
TO_TIMESTAMP(Start_Date, 'MM/DD/YYYY HH:MM:SS AM') AS start_TS
FROM db.info_table itab
Please advise.
You have two problems.
MI is the format for minutes, MM is for months (you have it twice, this is why you are getting that error).
Your date/time string has single digit values for month, day, etc. You can use a pretty simple regex for that.
select to_timestamp(regexp_replace('8/3/2022 4:58:49 PM', '\b([0-9])\b', '0\1'), 'MM/DD/YYYY HH:mi:SS AM')
TO_TIMESTAMP returns a TIMESTAMP(6). If you don't want microseconds you can specify the precision using
CAST(RegExp_Replace(start_date, '\b([0-9])\b', '0\1') AS timestamp(0) FORMAT 'MM/DD/YYYYbHH:Mi:SSbT')
All you need is pad day and month in Teradata (as opposed to Oracle etc). m/d/y format has not been implemented.
select '8/3/2022 4:58:49 PM' date1,
to_timestamp(
lpad(strtok(date1,'/',1),2,'0')||'/'||lpad(strtok(date1,'/',2),2,'0')||'/'||strtok(date1,'/',3)
,'mm/dd/yyyy hh24:mi:ss AM'
);

correct to_char date syntax to have trailing zeroes after milliseconds

My current query in oracle sql for getting a timestamp format is TO_CHAR(c2.start_on,'DD-MM-YY HH:MI:SS.FF PM'), it outputs the timestamp like this 25-11-20 07:00:13.36 PM
However I want it to display the date in this way 25-11-20 07:00:13.360000000 PM
What should I add in the timestamp format for this to be possible ?
I have tried doing it like this HH:MI:SS.FM00000 as suggested here
but it gives me the error. ORA-01821: date format not recognized
what is the correct way to get the date in the desired format ?
If you want fractional seconds, you don't want a DATE, you want a TIMESTAMP. So here's a timestamp formatted with 6 digits of precision
select to_char(systimestamp, 'HH:MI:SS.FF6') from dual;
If you have a date, you could convert it to a TIMESTAMP (using CAST AS TIMESTAMP), but better to look at updating your data model to use the proper type for the source column as starters.

Convert Varchar2 24h time to 12h format SQL

I have a Varchar2 column in a table that contains a time like '13:24:02'. I want to convert the value into 12h format with am/pm eg. 01:24:02 PM
Is there any way to do it? Tried to_char and to_date but isn't giving me the result that I wanted.
Convert it to a date and then back to a string in the correct format:
SELECT TO_CHAR(TO_DATE(your_column, 'HH24:MI:SS'), 'HH12:MI:SS AM')
AS formatted_time
FROM your_table
Which, for the sample data:
CREATE TABLE your_table (your_column) AS
SELECT '13:24:02' FROM DUAL;
Outputs:
FORMATTED_TIME
01:24:02 PM
db<>fiddle here

Hours and minutes between 2 incorrectly formatted datetimes

So i have some timestamps in a DB and i want to get the hours and minutes difference from them
The problem is the timestamp portion is formatted incorrectly where the hour is always 12 and the minutes portion is actually the hours and the seconds is actually the minutes.
Example DB timestamp: 10/1/2020 12:08:52 AM
So in the above example the time is actually 8:52 AM not 12:08 AM
How can i convert this datetime to something i can use in order to calculate the difference in minutes and hours between these 2 oddly formatted timestamps?
My ideal end goal is something that displays the difference in the HH:MM format
EDIT: the timestamps in oracle actually look like below, and in this eaxmple the 12 means nothing and 18 is actually the hours.
Example of what I'm looking for:
01-OCT-20 12.18.44.000000000 AM - 01-OCT-20 12.12.42.000000000 AM
Output: 06:02 . so the timespan would be 6 hours and 2 minutes in this case.
Thanks,
You can turn your string to an Oracle date (resp timestamp) with to_date() (resp to_timestamp()):
to_timestamp(mystring, 'dd/mm/yyyy ss:hh12:mi am')
Then you can use date arithmetics to compute the difference. Substrating timestamps gives you an interval, which is pretty much what you seem to be looking for, so:
to_timestamp(mystring1, 'dd/mm/yyyy ss:hh12:mi am')
- to_timestamp(mystring2, 'dd/mm/yyyy ss:hh12:mi am')
as myinterval
Like so?
(my default date format is 'yyyy-mm-dd hh24:mi:ss' in Oracle ...)
WITH
indata(sdb) AS (
SELECT '10/1/2020 12:08:52 AM' FROM dual
UNION ALL SELECT '10/1/2020 12:08:52 PM' FROM dual
)
SELECT
TO_TIMESTAMP(sdb,'dd/mm/yyyy 12:hh:mi AM') AS ts
FROM indata;
-- out ts
-- out ---------------------
-- out 2020-01-10 08:52:00
-- out 2020-01-10 20:52:00

How to extract time in HH24:MM from varchar in Oracle

I have a column in the following varchar format. I would like to extract the time based on a condition e.g. < 7:00.
Table1
Column: timer(varchar)
23:45
05:00
07:00
22:00
Expected output
test
05:00
07:30
I tried the following:
Select *
FROM Table1
where timer < 7:00
However, the result is not as expected.
Oracle does not have a time date, so presumably the type is a string.
Use string comparisons:
where time < '07:00'
Note that the leading 0 is important!
If this is just time and you want a proper comparison then you can convert them to date and compare them.
Select *
FROM Table1
where to_date(timer,'hh24:mi') < to_date('07:00','hh24:mi');
please note that your expected output contains 07:30 but it is not less than 07:00 so it will not be part of the output if you compare it with less than 07:00.
Cheers!!