Oracle timestamp issue to convert date - sql

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;

Related

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

How to truncate seconds from timestamp in postgres?

I have the following field in my table:
VISIT_DATE TIMESTAMP(0)
And it is holding time like that:
2022-01-13 11:04:15
Could someone tell me is it possible to cut off that seconds? I really don't need them.
I want to hold time in the following format:
2022-01-13 11:04
Either truncate the timestamp by minutes using date_trunc, which will return a timestamp without seconds, or use to_char if it is only about formatting the output:
SELECT date_trunc('minute',VISIT_DATE) FROM t;
SELECT to_char(VISIT_DATE,'yyyy-mm-dd hh24:mi') FROM t;
Demo:
Using date_trunc (result as timestamp):
SELECT date_trunc('minute','2022-01-13 11:04:15'::timestamp);
date_trunc
---------------------
2022-01-13 11:04:00
(1 Zeile)
Using to_char (result as text):
SELECT to_char('2022-01-13 11:04:15'::timestamp,'yyyy-mm-dd hh24:mi');
to_char
------------------
2022-01-13 11:04

Casting Local Time to UTC Formating Incorrect

HIRE_DATE is in a 'DATE' column. The timestamp is local (Los Angeles); I would like to convert it to UTC.
I can't for the life of me fathom why the UTC output is mangled (Last 2 digits of YY is the DD; and vice-versa) -- and the time does not convert to UTC.
HIRE_DATE: 30/04/2019 12:00:00 AM
select from_tz(to_timestamp(HIRE_DATE,'DD-MM-YY HH24:MI:SS'), 'America/Los_Angeles') at time zone 'UTC' from TABLE
OUTPUT: 19/04/2030 12:00:00 AM
If HIRE_DATE is a DATE data type then you don't need TO_TIMESTAMP.
TO_TIMESTAMP is used to convert a string (i.e. VARCHAR2) into a TIMESTAMP value but you have a DATE value.
Just do
select from_tz(CAST(HIRE_DATE AS TIMESTAMP), 'America/Los_Angeles') at time zone 'UTC'
from TABLE
Actually I don't understand why FROM_TZ does not accept DATE values whereas almost any other date/timestamp related function accept either DATE or TIMESTAMP value as input.
Note, the default output display format of this query is defined by current user session NLS_TIMESTAMP_TZ_FORMAT setting. If you are not satisfied with the output format, either change NLS_TIMESTAMP_TZ_FORMAT setting by executing ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = '...' or use TO_CHAR function to set output format explicitly.
Instead of
... AT TIME ZONE 'UTC'
you can also use
SYS_EXTRACT_UTC(...)
The upper returns a TIMESTAMP WITH TIME ZONE value, the second one returns a TIMESTAMP value.
Would this do any good?
SQL> select from_tz(cast (sysdate as timestamp), 'UTC') result from dual;
RESULT
---------------------------------------------------------------------------
27.09.20 10:59:28,000000 UTC
Or, in your case
select from_tz(cast (hire_date as timestamp), 'UTC' from dual
No need to apply any format mask to hire_date as it is a DATE datatype (at least, that's what you said).
You use the word "convert" which can mean one of two things:
change the data type, which is what FROM_TZ does
change the value from one time zone to another, which FROM_TZ does not do.
You didn't give your expected output, so we may misunderstand.
To change the data type:
with data(dte) as (
select date '2019-04-30' + interval '12' hour from dual
)
select from_tz(cast(dte as timestamp), 'America/Los_Angeles') from data
FROM_TZ(CAST(DTEASTIMESTAMP),'AMERICA/LOS_ANGELES')
30-APR-19 12.00.00.000000 PM AMERICA/LOS_ANGELES
To get the simultaneous datetime value in UTC:
with data(dte) as (
select date '2019-04-30' + interval '12' hour from dual
)
select cast(sys_extract_utc(from_tz(cast(dte as timestamp), 'America/Los_Angeles')) as date) from data
CAST(SYS_EXTRACT_UTC(FROM_TZ(CAST(DTEASTIMESTAMP),'AMERICA/LOS_ANGELES'))ASDATE)
2019-04-30 19:00:00

Convert Varchar with timezone to Timestamp in Oracle

I have a problem converting varchar to DateTime/timestamp.
Here is the case
ID EVENT_TIME(Varchar)
1 2020-04-12T09:25:53+0800
2 2020-04-12T09:25:53+0700
3 2020-04-12T09:25:53+0900
return I want, all timestamp convert to +0700
ID EVENT_TIME(Datetime)
1 2020-04-12 10:25:53
2 2020-04-12 09:25:53
3 2020-04-12 11:25:53
is this possible? and how can I do it using oracle?
thanks
Please use below query to convert varchar to timestamp using to_timestamp_tz and again convert it to the required time format using to_char
select ID, to_char(to_timestamp_tz(EVENT_TIME, 'YYYY-MM-DD"T"HH24:MI:SS.FF TZH:TZM'), 'YYYY-MM-DD HH24:MI:SS') as event_date from table_name;
Example:
select to_char(to_timestamp_tz('2020-04-12T09:25:53+0800', 'YYYY-MM-DD"T"HH24:MI:SS.FF TZH:TZM'), 'YYYY-MM-DD HH24:MI:SS') as event_date from dual;
As #Jim said, you can use to_timestamp_tz() with a character literal to convert the string to a timestamp value:
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
The to normalise to the +07:00 offset you can use at time zone:
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00'
If you don't want to keep the time zone part you can cast to a plain timestamp:
cast(
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00'
as timestamp)
Or you can cast (... as date) as you don't have fractional seconds.
Or you can convert that back to a string for display:
to_char(
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00',
'SYYYY-MM-DD HH24:MI:SS')
db<>fiddle

Inserting Date gives error ORA-01861: literal does not match format string

Insert date into WSH_Delivery_Details_Interface (https://docs.oracle.com/cloud/r13_update17c/scmcs_gs/OEDSC/WSH_DELIVERY_DETAILS_tbl.htm) throws this error
Query :
insert into WSH_DEL_DETAILS_Interface
(DELIVERY_DETAIL_INTERFACE_ID, CREATION_DATE, Date_Requested)
values
(30010985553,
TO_DATE('11/12/2018T05:10:30-00:00', 'DD/MM/YYYY '),
TO_DATE('11/12/2018', 'DD/MM/YYYY'));
Sample Record in creation_date and date_requested column:
Date_Requested Creation_Date
16-JUN-10 17-JUN-10 03.40.31.865000000 PM
The error can be reduced to:
select TO_DATE('11/12/2018T05:10:30-00:00','DD/MM/YYYY ') from dual;
Error report -
ORA-01861: literal does not match format string
Which is reasonable as it clearly doesn't match. You need to include the time elements in your format mask, and also a character literal for the fixed 'T', and for the fixed time zone offset:
select TO_DATE('11/12/2018T05:10:30-00:00','DD/MM/YYYY"T"HH24:MI:SS"-00:00"') from dual;
TO_DATE('11/12/2018
-------------------
2018-12-11 05:10:30
If that 'time zone' part isn't fixed and needs to be honoured, then you can use to_timestamp_tz() instead of to_date():
select TO_TIMESTAMP_TZ('11/12/2018T05:10:30-00:00','DD/MM/YYYY"T"HH24:MI:SS.FFTZH:TZM')
from dual;
TO_TIMESTAMP_TZ('11/12/20
-------------------------
2018-12-11 05:10:30.0 GMT
I've included .FF in the format model as well as the time zone offset elements (you could use TZR instead of TZH:TZM if you might be passed regions instead of offsets), since your example of existing data has fractional seconds, even though your literal string does not in this case
And you can cast() that to a date or plain timestamp if necessary, or possibly normalise to UTC if the input values can be other zones/offsets:
select TO_TIMESTAMP_TZ('11/12/2018T04:10:30-01:00','DD/MM/YYYY"T"HH24:MI:SS.FFTZH:TZM') as orig,
SYS_EXTRACT_UTC(
TO_TIMESTAMP_TZ('11/12/2018T04:10:30-01:00','DD/MM/YYYY"T"HH24:MI:SS.FFTZH:TZM')) as utc
from dual;
ORIG UTC
---------------------------- ---------------------
2018-12-11 04:10:30.0 -01:00 2018-12-11 05:10:30.0