Skip to content
Advertisement

AM/PM Conversion For Date Time

I’ve a very basic requirement that I did earlier but for now unable to resolve it. I’ve a table in database that stores in and out time as the following format:

8/18/2019 8:00:00 AM

So what I want is to convert the above into the below format:

8/18/2019 8:00:00 PM

Nothing complex here, just to convert from AM to PM. I tried something like this but not getting the exact result:

SELECT TO_DATE(TO_CHAR(M.IN_TIME, 'MM/DD/YYYY HH:MI:SS'), 'MM/DD/YYYY HH12:MI:SS PM') IN_TIME FROM Table m
WHERE M.DATE = '31-AUG-2019';

Anything that I missed here? Would appreciate if someone points out.

Advertisement

Answer

First of all, dates are stored in the internal format so you can not change it using what you are doing. What you see in database is the dates converted into the string.(human readable format)

You can try the following to achieve what you want:

SELECT CASE WHEN TRUNC(IN_TIME) + (12/24) > IN_TIME THEN
IN_TIME + (12/24) 
ELSE IN_TIME END
 AS IN_TIME_CONVERTED
FROM TABLE M
WHERE ....

This query will convert only dates before noon to equivalent in PM.

Cheers!!

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement