OracleSQL convert Date fields into another timezone - sql

in my Oracle DB I have a DATE column where store date values.
All date values are in TimeZone Europe/Berlin. Now the application changes its TimeZone to UTC, this means I need to convert all existing Dates from Europe/Berlin into UTC.
Is there a way to do this natively in Oracle?

Use FROM_TZ( timestampvalue, timezone ) to convert a timestamp to a timestamp at a specific time zone and then you can use AT TIME ZONE 'UTC' to convert it to the UTC time zone and cast it back to a date:
SELECT CAST(
FROM_TZ(
CAST( your_column AS TIMESTAMP ),
'Europe/Berlin'
)
AT TIME ZONE 'UTC'
AS DATE
)
FROM your_table;

Related

How to adjust timestamp timezone offset for YYYY-MM-DD''T''HH:mm:ss.SSSZ format timestamp in presto?

I have a dataset with timestamp strings like '2022-05-25T13:31:22.566-0400' I would like to convert it to '%Y-%m-%dT%H:%i:%s' format but adjusting for the timezone difference.
So for the above, how convert '2022-05-25T13:31:22.566-0400' to '2022-05-25T17:31:22.566' in Presto?
Thanks a lot!
You can use from_iso8601_timestamp to parse date, then cast to timestamp to remove timezone info (will be treated as UTC, at time zone 'UTC' should have the same effect) and use date_format to get required output format:
select date_format(
cast(from_iso8601_timestamp('2022-05-25T13:31:22.566-0400') as timestamp),
'%Y-%m-%dT%H:%i:%s')
Or
select date_format(
from_iso8601_timestamp('2022-05-25T13:31:22.566-0400') at time zone 'UTC',
'%Y-%m-%dT%H:%i:%s')
Output:
_col0
2022-05-25T17:31:22

In Redshift, How to cast current_timestamp to timestamp with UTC timezone?

This is working:
select to_char(current_timestamp AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS OF')
But when I am inserting it into a table it is failing with this error:
42804: column "load_time" is of type timestamp with time zone but expression is of type text
The to_char() function outputs a text string.
If you are trying to insert it into a TIMESTAMP WITH TIME ZONE field, then simply refer to:
current_timestamp AT TIME ZONE 'UTC'

How to convert unix timestamp to date with time zone?

How can I convert for example timestamp:
1559347196759
to datetime with time zone 'GMT+2' using PostgreSQL?
I believe this does what you want:
select (timestamp 'epoch' + 1559347196759 * interval '1 millisecond') at time zone '+02:00'
The first expression converts the value to UTC. The second shifts the timezone.
Convert it to a timestamp with time zone (absolute time) and convert it to the appropriate time zone:
SELECT to_timestamp(1559347196759 / 1000.0) AT TIME ZONE '-02';
timezone
-------------------------
2019-06-01 01:59:56.759
(1 row)

Issue with date in oracle

I am trying to pull date from one table which is in ISO format and then store it in US date format?
Trunc(cast(to_timestamp(ATTRIBUTE_39,'yyyy-mm-dd"T"hh24:mi:ss.ff3"Z"')as date))
Actual output:
4/11/2018 12:00:00 AM
expected output:
4/11/2018
I am trying to pull date from one table which is in ISO format and then store it in US date format?
Dates (and timestamps) do not have a format - they are represented in a table by 7 bytes for a date or 20 bytes for a timestamp.
You can get the value as a DATE data type using TO_TIMESTAMP_TZ and either the TZR or TZH:THM format models to match the time zone region/offset (they will both work with the Zulu time zone region)
SELECT CAST(
TO_TIMESTAMP_TZ(
ATTRIBUTE_39,
'yyyy-mm-dd"T"hh24:mi:ss.ff3TZH:TZM'
) AT TIME ZONE 'UTC' -- Convert to a common time zone
AS DATE
)
FROM your_table;
When you select from the table then whatever client program you are using (typically) will implicitly convert the 7-bytes it uses internally to something you, the user, can read - a string. SQL/Plus and SQL Developer use the NLS_DATE_FORMAT session parameter as the format model when they perform this implicit conversion.
So your query is effectively converted to:
SELECT TO_CHAR(
CAST(
TO_TIMESTAMP_TZ(
ATTRIBUTE_39,
'yyyy-mm-dd"T"hh24:mi:ss.ff3TZR'
) AT TIME ZONE 'UTC'
AS DATE
),
(
SELECT VALUE
FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = 'NLS_DATE_FORMAT'
)
)
FROM your_table;
If you want to format a date or a timestamp then you will have to explicitly convert it to a string using TO_CHAR():
SELECT TO_CHAR(
CAST(
TO_TIMESTAMP_TZ(
ATTRIBUTE_39,
'yyyy-mm-dd"T"hh24:mi:ss.ff3TZR'
) AT TIME ZONE 'UTC'
AS DATE
),
'MM/DD/YYYY'
)
FROM your_table;
Or by altering the NLS_DATE_FORMAT session parameter:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
(Be aware that this will only change the format in the current session and will not change it for any other sessions/users.)
Try this:
select to_char(Trunc(cast(to_timestamp('2016-06-29T13:13:00.123','yyyy-mm-dd"T"hh24:mi:ss.ff3"Z"')as date)),'MM/DD/YYYY') from dual
the default date/time formatting depends on your NLS_DATE_FORMAT setting.

I want to adjust a date for summer/winter time and time zone when insterting it into a table

I have a date and time variable in TABLE_A that is in GMT. I want to insert this date and time into TABLE_B, but I want the insterted value to be adjusted for time zone and summer/winter time.
That is:
INSERT into TABLE_A (ADJUSTED_DATE_AND_TIME)
SELECT GMT_DATE_AND_TIME [Perform proper adjustments here..?]
FROM TABLE_A
Can I do this? In that case, how do I write ?
Thank.
I think you can simply convert the GMT/UTC time. However, you have to take the full region name of your time zone.
SELECT TIMESTAMP '2014-06-10 12:00:00 +00:00' AT TIME ZONE 'Europe/Zurich' AS summer FROM dual;
SUMMER
---------------------------------------
10.06.2014 14:00:00.000000000 +02:00
SELECT TIMESTAMP '2014-12-10 12:00:00 +00:00' AT TIME ZONE 'Europe/Zurich' AS winter FROM dual;
WINTER
---------------------------------------
10.12.2014 13:00:00.000000000 +01:00
Since your source value is data type DATE you have to do following steps.
Cast DATE to TIMESTAMP
Set Time zone of the value using FROM_TZ
Convert the value to new time zone using AT TIME ZONE '...'
Cast the value to DATE
Written in a single statement it is
select
CAST(FROM_TZ(CAST(sy_sttime AS TIMESTAMP), 'UTC') AT TIME ZONE 'Europe/Zurich' AS DATE)
from sy_request
or a bit less clear
select
CAST((CAST(sy_sttime AS TIMESTAMP) AT TIME ZONE 'UTC') AT TIME ZONE 'Europe/Zurich' AS DATE)
from sy_request