Convert Varchar2 24h time to 12h format SQL - 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

Related

Oracle timestamp issue to convert date

I am stuck in a data warehousing task where I need to map a column sourced as varchar2 datatype, and a target in TIMESTAMP (6) WITH TIME ZONE datatype.
Data format contained in source: 2019-08-20
I tried using this mapping:
select to_char(TO_TIMESTAMP_TZ('2019-08-20', 'yyyy-mm-dd"T"hh:mi:sstzhtzm'),'yyyy-mm-dd hh:mi:ss AM tzh:tzm') from dual;
which actually works for dual table.
But when the data loads in target this is the output given:
12/1/9999 12:00:00.000000 AM +00:00
Desired and expected output:
2019-08-20 12:00:00 AM +02:00
As you have only precision till date available with you in the varchar2, all other part of the timestamp (hour, minute, second and fraction of second) will be 0.
Try this:
select TO_TIMESTAMP_TZ('2019-08-20', 'yyyy-mm-dd') from dual;
db<>fiddle demo
Cheers!!
Why do you do TO_CHAR if you're inserting into a TIMESTAMP column?
Just insert this:
select TO_TIMESTAMP_TZ('2019-08-20', 'yyyy-mm-dd"T"hh:mi:sstzhtzm') from dual;

01830. 00000 - "date format picture ends before converting entire input string"

select to_date('13/03/17 05:43:29,000000000 PM -05:00DD/MM/YY HH24:MI:SS') from
irregularities;
How to convert this date to 24-hour format?
You can convert a string to a timestamp with time zone using:
select to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
from dual;
If you only want a date data type then you can cast it:
select cast(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
as date)
from dual;
If you really only want the string version you can convert it back, which you would usually only do for display:
select to_date(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM'),
'DD/MM/YYYY HH24:MI:SS')
from dual;
If the original string is coming from a table then just replace the text literal with the column name, and dual with your table name. Of course, that assumes the column is actually a string. If it is actually already a timestamp and your client is just displaying it in a way you don't like, you only need theto_char() part.
Read more about these things in the documentation: to_timestamp_tz, format models, cast() and to_char().

How to convert a given date with a specified date format using dual table on Oracle SQL

Below is the example
2016-02-06T06:06:41+00:00
will be converted to
2/6/2016 6:06:00 AM
Thanks in advance
Use TO_CHAR to get a formatted string from a timestamp:
select to_char(timestamp '2016-02-06 06:06:41 +00:00',
'fmdd/mm/yyyy fmhh:mi:ss am') from dual;
(The FM in the format exist to suppress leading zeros.)

convert string literal to a date

I have a varchar2 field in my db with the format of for example -
2015-08-19 00:00:01.0
2014-01-11 00:00:01.0
etc.
I am trying to convert this to a date of format DD-MON-YYYY. For instance, 2015-08-19 00:00:01.0 should become 19-AUG-2015. I've tried
select to_date(upgrade_date, 'YYYY-MM-DD HH24:MI:SS') from connection_report_update
but even at this point I'am getting ORA-01830 date format ends before converting the entire input string. Any ideas?
You have details upto milli seconds, for which, you have to use TO_TIMESTAMP() with format model 'FF'
select to_timestamp('2015-08-19 00:00:01.0' ,'YYYY-MM-DD HH24:MI:SS.FF') as result from dual;
RESULT
---------------------------------------------------------------------------
19-AUG-15 12.00.01.000000000 AM
And Date doesn't have a format itself, only the date output can be in a format. So, when you want it to be printed in a different format, you would need to again use a TO_CHAR() of the converted timestamp;
select to_char(to_timestamp('2015-08-19 00:00:01.0' ,'YYYY-MM-DD HH24:MI:SS.FF'),'DD-MON-YYYY') as result from dual;
RESULT
-----------
19-AUG-2015
Why do you store datetimes in a string???
Anyhow. To get from '2015-08-19 00:00:01.0' to a datetime with milliseconds (which is a TIMESTAMP in Oracle) use to_timestamp:
to_timestamp('2015-08-19 00:00:01.0', 'yyyy-mm-dd hh24:mi:ss.ff')
Then to get the desired output format, use to_char:
to_char(thedate, 'DD-MON-YYYY')
Together:
to_char(to_timestamp('2015-08-19 00:00:01.0', 'yyyy-mm-dd hh24:mi:ss.ff'), 'DD-MON-YYYY')
You should be specifying the format that you want in the call of to_date not the current format:
select to_date(upgrade_date, 'DD-MM-YYYY') from connection_report_update

Convert number to date sql oracle

I'm trying to convert a number (yyyymmdd) to date (mm/dd/yyyy)
For example
20150302 ====> 03/02/2015
You can try this:
select to_date(20150302,'yyyymmdd') from dual;
or
select to_char(to_date(20150302,'yyyymmdd'),'mm/dd/yyyy') from dual;
You can use TO_DATE function to convert NUMBER to DATE. Try in following:
SELECT TO_DATE(20150302, 'YYYYMMDD') FROM DUAL
The above answer is still incorrect.
It returns a character string when a date is needed.
The correct way is:
select to_date(to_char(20210416), 'YYYYMMDD') num_to_char_to_date from dual;
TO_DATE accepts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype and converts into a value of DATE datatype.
So, convert the number into string, and apply to_date. You could use single-quotation marks around the number to convert it into string.
SELECT TO_DATE('20150302', 'YYYYMMDD') FROM dual;
Remember, a DATE has no format, what you see is for display purpose. If you want to display the date in a desired format, then use TO_CHAR along with your desired format model.
SELECT TO_CHAR(TO_DATE('20150302', 'YYYYMMDD'), 'mm/dd/yyyy') FROM dual;
Read more about TO_DATE.
To convert in the required format:
select to_char(to_date(20150302, 'YYYYDDMM'), 'mm/dd/yyyy')
select to_char(to_date(20220912, 'YYYYDDMM'), 'YYYYMMDD') from dual