Oracle - how to convert datetime to TZ format? - oracle

I have this time format:
2020-09-08 14:00:00
and I want to convert it to following which is from Sydney time to UTC:
2020-09-08T01:00:00Z

First of all, giving the answer considering that your data is actual varchar2:
SQL> SELECT TO_CHAR(CAST(TO_DATE('2020-09-08 14:00:00','YYYY-MM-DD HH24:MI:SS')
2 AS TIMESTAMP) AT TIME ZONE 'Australia/Sydney',
3 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
4 FROM DUAL;
TO_CHAR(CAST(TO_DATE
--------------------
2020-09-08T18:30:00Z
SQL>
Please note that You can also set NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD"T"HH24:MI:SS"Z"' in your session (and don't use the TO_CHAR in your query) as per your requirement. Also, you can use UTC timezone instead of Australia/Sydney if you want to convert your local date to UTC timezone. DB<>Fiddle for UTC conversion.
If the given data is already in date datatype stored in your table, then you just need to use CAST as follows: (TO_DATE is not required)
SELECT TO_CHAR(CAST(YOUR_DATE_COLUMN
AS TIMESTAMP) AT TIME ZONE 'Australia/Sydney',
'YYYY-MM-DD"T"HH24:MI:SS"Z"')
FROM YOUR_TABLE;

Related

Why is DATE data type treated like TIMESTAMP(0) data type in Oracle mode in H2?

According to the H2 documentation, in the Oracle compatibility mode:
DATE data type is treated like TIMESTAMP(0) data type.
Meantime, DATE and TIMESTAMP(0) datatypes are not the same in Oracle. Compare:
SELECT CAST(SYSDATE AS TIMESTAMP(0)), CAST(SYSDATE AS DATE) from dual
gives
25-MAR-22 13.07.42.000000000 25-MAR-22
respectively.
In particular, this weird treating of DATE as TIMESTAMP(0) influences on how H2 calculates the difference between two dates.
Again, in Oracle:
SELECT CAST(TO_DATE('2022-01-05', 'YYYY-MM-DD') AS TIMESTAMP(0)) - CAST(TO_DATE('2022-01-01', 'YYYY-MM-DD') AS TIMESTAMP(0)) from dual
gives
+04 00:00:00.000000
and
SELECT CAST(TO_DATE('2022-01-05', 'YYYY-MM-DD') AS DATE) - CAST(TO_DATE('2022-01-01', 'YYYY-MM-DD') AS DATE) from dual
produces just:
4
Apparently, for H2 both above queries produce the result in nanoseconds and not days as expected.
So, it is an H2 bug or I am missing something?
Meantime, DATE and TIMESTAMP(0) datatypes are not the same in Oracle
Oracle differs from many other RDBMS in that its DATE data type ALWAYS contains both a date and a time component. Its implementation predates the ANSI standard.
In Oracle, if you have the table:
CREATE TABLE table_name (ts TIMESTAMP(0), dt DATE);
and insert the data:
INSERT INTO table_name (ts, dt) VALUES (SYSDATE, SYSDATE);
Then you can look at the binary data being stored using the DUMP function:
SELECT DUMP(ts) AS dump_ts,
DUMP(dt) AS dump_dt
FROM table_name;
Which outputs:
DUMP_TS
DUMP_DT
Typ=180 Len=7: 120,122,3,25,15,13,37
Typ=12 Len=7: 120,122,3,25,15,13,37
Then you can see that they are both stored as 7-byte binary values:
120 = Century + 100
122 = Year-of-century + 100
3 = Month
25 = Day
15 = Hour + 1
13 = Minutes + 1
37 = Seconds + 1
And the binary values are identical (the only difference is in the meta-data Typ where 180 = TIMESTAMP and 12 = DATE).
Effectively, they are stored identically.
db<>fiddle here
However
The side-effects of a TIMESTAMP vs. a DATE data type in Oracle may lead to different effects.
When you subtract a TIMESTAMP and either a TIMESTAMP or a DATE then the return value is an INTERVAL DAY TO SECOND data type.
When you subtract a DATE and a DATE then the default return value is a NUMBER representing the number of days difference.
When you display a TIMESTAMP then the client application you are using may default to using the NLS_TIMESTAMP_FORMAT session parameter to format the timestamp as a string and the default for this parameter will typically show date, time and fractional seconds.
When you display a DATE then the client application you are using may default to using the NLS_DATE_FORMAT session parameter to format the date as a string and the default for this parameter will show date but not time (and there will never be any fractional seconds to show). Just because the client application may chose not to show the time component does not mean that the time component does not exist.
If you set the session parameters using:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Then, provided your client application is using those parameters to format them, they will display identically.
The problem you are seeing with the difference in Oracle is due to these side effects.
If the question is
So, it is an H2 bug or I am missing something?
than the the answer would be:
No, it is not a bug, and what you've missed is the fact, that compatibility modes in H2 are just that - attempt to reach with minimal efforts maximum compatibility with different databases.
H2 is not an emulator for any non-standard features (quirks) of those databases.
It that particular case, to achieve identical behaviour would require to introduce new non-standard data type, which goes beyond "minimal effort" level.
The different in the output of the values in the first query is down to the session's NLS settings. These control the display format for dates and timestamps:
sho parameter nls_date_format
NAME TYPE VALUE
--------------- ------ -----------
nls_date_format string DD-MON-YYYY
sho parameter nls_timestamp_format
NAME TYPE VALUE
-------------------- ------ -------------------------
nls_timestamp_format string DD-MON-YYYY HH24.MI.SSXFF
SELECT CAST(SYSDATE AS TIMESTAMP(0)), CAST(SYSDATE AS DATE) from dual;
CAST(SYSDATEASTIMESTAMP(0)) CAST(SYSDAT
------------------------------ -----------
25-MAR-2022 12.18.24.000000000 25-MAR-2022
If you change these to be the same format, both expressions return the same result:
alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS';
alter session set nls_timestamp_format = 'DD-MON-YYYY HH24:MI:SS';
SELECT CAST(SYSDATE AS TIMESTAMP(0)), CAST(SYSDATE AS DATE) from dual;
CAST(SYSDATEASTIMEST CAST(SYSDATEASDATE)
-------------------- --------------------
25-MAR-2022 12:17:43 25-MAR-2022 12:17:43
So they both contain the full date + time with no fractional seconds.
Note that while date and timestamp(0) have the same precision, as your further examples show they work differently:
Subtracting one date from another returns the number of days between the values as a number
Subtracting a timestamp from a date or timestamp returns an interval
So the result of:
SELECT CAST(TO_DATE('2022-01-05', 'YYYY-MM-DD') AS DATE) - CAST(TO_DATE('2022-01-01', 'YYYY-MM-DD') AS DATE) from dual
Is 4 days.

Convert timezone format in Oracle

I need to convert below timezone format in the following format:
Input:
2020-10-28T20:12:20.986Z
Output:
28-OCT-20 8:12 PM
I tried below query but I am unable to get timestamp with it. Please help.
select TO_TIMESTAMP(SUBSTR('2020-04-21T13:02:31.259Z',1,(INSTR('2020-04-21T13:02:31.259Z', 'T') - 1)),'YYYY-MM-DD HH24:MI:SS') from dual;
One option might be this
SQL> alter session set nls_timestamp_format = 'dd-MON-YY hh:mi PM' ;
Session altered.
SQL> select to_timestamp('2020-10-28T20:12:20.986Z','yyyy-mm-dd"T"hh24:mi:ss.ff3"Z"') from dual ;
TO_TIMESTAMP('2020-10-28T20:12:20.986Z','YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
---------------------------------------------------------------------------
28-OCT-20 08:12 PM
SQL>
But if you rely better in the to_timestamp function without any session setting, then it is better
SQL> select to_timestamp('2020-10-28T20:12:20.986Z','yyyy-mm-dd"T"hh24:mi:ss.ff3"Z"') from dual ;
TO_TIMESTAMP('2020-10-28T20:12:20.986Z','YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
---------------------------------------------------------------------------
28-OCT-20 08.12.20.986000000 PM
You have a timestamp string with a time zone, use TO_TIMESTAMP_TZ rather than TO_TIMESTAMP and then use TO_CHAR to format it:
SELECT TO_CHAR(
TO_TIMESTAMP_TZ(
'2020-04-21T13:02:31.259Z',
'YYYY-MM-DD"T"HH24:MI:SS.FFTZR'
),
'DD-MON-RR HH12:MI AM',
'NLS_DATE_LANGUAGE=American'
)
FROM DUAL;
db<>fiddle here
Note: DATE, TIMESTAMP and TIMESTAMP WITH TIME ZONE are binary data types and are stored in 7-20 bytes (1 byte each for century, year-of-century, month, day, hour, minute and second then up to 6 optional bytes for fractional seconds for TIMESTAMPs and up to 7-bytes for time zone for TIMESTAMP WITH TIME ZONE). It is never stored in any particular format.
How the DATE/TIMESTAMP data types are displayed is dependent on the client application that you are using to query the database; some may use the NLS settings for the user's session but others do not use that. If you want a particular format then convert the DATE/TIMESTAMP to a string using TO_CHAR.

create oracle date with time stamp from HH24 string

I have hour in 'HH24' format. For example: '13:35' I need to convert this into today's date of '13:35' in the time stamp - "dd/mm/yyyy HH24:MI:SS".
To_timeStamp() method returning me the date with timestamp of starting of the month.
It is returning "01-Jul-2019 13:35:00", please help me to get today's date with the exact hour and minutes?
You need to concatenate the formatted date with your input and then convert that back to a timestamp:
select to_timestamp(to_char(sysdate, 'yyyy-mm-dd')||' 13:35', 'yyyy-mm-dd hh24:mi')
from dual;
If that time value is a column in your table, use that instead of the constant value:
select to_timestamp(to_char(sysdate, 'yyyy-mm-dd')||' '||the_time_column, 'yyyy-mm-dd hh24:mi')
from your_table;

Oracle timestamp cast with local time zone has different results in / out DST date range

While checking timestamp transformation i'm stumbling over different timezone conversion in or out DST range.
Knows Oracle the range from end of march to end of october?
Are these results correctly?
SELECT DBTIMEZONE FROM dual;
+02:00
SELECT
TO_CHAR(CAST(TO_TIMESTAMP_TZ('2018-02-25T22:22:22+01:00','YYYY-MM-DD"T"HH24:MI:SS TZR') as timestamp with local time zone)),
TO_CHAR(CAST(TO_TIMESTAMP_TZ('2020-04-28T20:20:20+01:00','YYYY-MM-DD"T"HH24:MI:SS TZR') as timestamp with local time zone))
FROM DUAL;
OUTSIDE_DST INSIDE_DST
25.02.18 22:22:22,000000 28.04.20 21:20:20,000000
Data type TIMESTAMP WITH LOCAL TIME ZONE means the time is always and only shown in current user session time zone, i.e. SESSIONTIMEZONE
Your statement
SELECT
CAST(TO_TIMESTAMP_TZ('2018-02-25T22:22:22+01:00','YYYY-MM-DD"T"HH24:MI:SS TZR') as timestamp with local time zone),
CAST(TO_TIMESTAMP_TZ('2020-04-28T20:20:20+01:00','YYYY-MM-DD"T"HH24:MI:SS TZR') as timestamp with local time zone)
FROM DUAL;
is actually equal to
SELECT
TIMESTAMP '2018-02-25 22:22:20+01:00' AT TIME ZONE SESSIONTIMEZONE,
TIMESTAMP '2020-04-28 20:20:20+01:00' AT TIME ZONE SESSIONTIMEZONE
FROM DUAL;
Values are returned in current user session time zone, there is nothing wrong with it. +01:00 means always 1 hour ahead UTC, no matter if DST is active or not.
Your SESSIONTIMEZONE is most likely a region name like "continent/city". Try to set your session time zone for example to
ALTER SESSION SET TIME_ZONE = '+02:00';
then the result will be all in +02:00
You can interrogate Daylight-Saving times with TZD, for example:
SELECT
TO_CHAR(TIMESTAMP '2018-02-25 22:22:20+01:00' AT TIME ZONE SESSIONTIMEZONE,
'yyyy-dd-mm- hh24:mi:ss TZD') AS ts1,
TO_CHAR(TIMESTAMP '2020-04-28 20:20:20+01:00' AT TIME ZONE SESSIONTIMEZONE,
'yyyy-dd-mm- hh24:mi:ss TZD') AS ts2
FROM DUAL;
TS1 TS2
------------------------------- --------------------------------------------
2018-25-02- 22:22:20 CET 2020-28-04- 21:20:20 CEST
1 row selected.
The first time has no daylight saving (i.e. winter-time), the second one does (i.e. summer-time)

datatype date and time for oracle database

Which type of data type are used to insert date and time in this format
(10-Oct-2013, 04:00 PM) for oracle database..
CREATE TABLE OPERATOR (
LASTPSWDCHANGE DATE,
LASTSIGNONDTTM DATE,
LASTUPDDTTM DATE
);
DATE is the correct type to store date/time values.The DATE data type does not in itself specify any particular format when converting to or from a string.
To convert from string (usually VARCHAR2) to DATE use
TO_DATE(<string with date>, <date format>)
To convert from DATE to VARCHAR2 use
TO_CHAR(<date>, <date format>)
There is a default date format which is determined by the locale of the client. In tools like Toad or SQL developer the default format often doesn't include the time part.
DATE has second precision. For higher precision (millisecond, nanosecond etc) use TIMESTAMP.
EDIT:
You can find documentation on the format specifiers on Oracles website.
In your case, use:
TO_DATE('10-Oct-2013, 04:00 PM', 'DD-MON-YYYY, HH:MI PM')
TIMESTAMP data type can be used here
a TIMESTAMP := TIMESTAMP '2013-10-10 16:00:00';
b TIMESTAMP WITH TIME ZONE := TIMESTAMP '2013-10-10 16:00:00.00 +02:00';
Hope this helps.
You can use DATE as datatype. But you can retrieve date in various format using TO_CHAR function.
An example:
SELECT TO_CHAR( LASTPSWDCHANGE ,'DD-Mon-YYYY, HH:MI AM' ),
TO_CHAR( LASTSIGNONDTTM ,'DD-Mon-YYYY, HH:MI AM' ),
TO_CHAR( LASTUPDDTTM , 'DD-Mon-YYYY, HH:MI AM' )
FROM OPERATOR

Resources