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

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'

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

How can I specify timezone with current_timestamp in Oracle

How can I specify a timezone when calling the CURRENT_TIMESTAMP function in Oracle? Note that I want to do it programmatically and not by setting some environment variable.
Use AT TIME ZONE:
SELECT CURRENT_TIMESTAMP AT TIME ZONE 'UTC' AS utc_time,
CURRENT_TIMESTAMP AT TIME ZONE 'PST' AS pst_time
FROM DUAL;
Which outputs:
UTC_TIME
PST_TIME
18-SEP-21 20.02.58.762964 UTC
18-SEP-21 13.02.58.762964 PST
db<>fiddle here

OracleSQL convert Date fields into another timezone

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;

date/time Conversion between different timezones

I am using the following sql to covert a date/time value from one timezone to another
from_tz(cast(to_date(to_char(q.created_date, 'DDMMYYYY:HH24:MI:SS'),
'DDMMYYYY:HH24:MI:SS') as timestamp), 'Europe/London') at time zone 'America/New_York'
else null end as Message_Rcd_Date_Time
output from the above is as follows:
29-OCT-2016 14:28:16.000000 -04:00
What I want to do is output this date/time as shown below, excluding the timezone, can you please tell me what I need to change in order to achieve this?
29-OCT-2016 14:28:16
First let's dissolve your expression
FROM_TZ(CAST(TO_DATE(TO_CHAR(q.created_date, 'DDMMYYYY:HH24:MI:SS'), 'DDMMYYYY:HH24:MI:SS') AS TIMESTAMP), 'Europe/London') AT TIME ZONE 'America/New_York'
does following:
TO_CHAR(q.created_date, 'DDMMYYYY:HH24:MI:SS') -> Convert created_date value to VARCHAR2
TO_DATE(..., 'DDMMYYYY:HH24:MI:SS') -> Convert it back to a DATE
CAST(... AS TIMESTAMP) -> Convert it to a TIMESTAMP (without time zone)
FROM_TZ(..., 'Europe/London') -> Attach time zone 'Europe/London' to it
... AT TIME ZONE 'America/New_York' -> Convert to time zone 'America/New_York'
Point 1,2 and 3 are useless! Since created_date is a TIMESTAMP you can do it shorter
TO_CHAR(FROM_TZ(q.created_date, 'Europe/London') AT TIME ZONE 'America/New_York', 'DD-MON-YYYY HH24:MI:SS')
In case your SESSIONTIMEZONE is Europe/London you can even make
TO_CHAR(q.created_date AT TIME ZONE 'America/New_York', 'DD-MON-YYYY HH24:MI:SS')
Not sure about Oracle but below query can help.
SELECT SUBSTR((cast(to_date(to_char(q.created_date, 'DDMMYYYY:HH24:MI:SS'),
'DDMMYYYY:HH24:MI:SS') as timestamp), 'Europe/London') , 1, 20) at time zone 'America/New_York'
else null end as Message_Rcd_Date_Time;
If it would have been SQL Server only LEFT function would have worked.

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