Extracting timestamp from timestamp with time zone Presto - sql

Is there a native Presto function that provides support to extract the timestamp from a timestamp with time zone?
Taking something like this
SELECT
PARSE_DATETIME('2022-03-13+02:00:99 UTC', 'yyyy-MM-dd+HH:mm:99 ZZZ') AT TIME ZONE 'UTC' AS utc_time
Which returns a value of:
2022-03-13+02:00:99 UTC
To:
2022-03-13+02:00:99
I couldn't find information in the docs for this kind of support. It seems as though my only option is to convert this as a varchar, remove the ' UTC' characters, and then re-convert this into a timestamp if I want to remove the time zone suffix from the timestamp with time zone

You can just cast to timestamp:
SELECT cast(PARSE_DATETIME('2022-03-13+02:00:99 UTC', 'yyyy-MM-dd+HH:mm:99 ZZZ') as timestamp) utc_time
Output:
utc_time
2022-03-13 02:00:00.000

Related

Combining date and UTC time fields in Postgres

I have 2 separate fields for date and time. The time field is stored in UTC time. How can I combine the 2 into a datetime field into local time
Example:
date: 2021-03-08
time in UTC: 23:00
time zone: GMT+8
I would like to get 2021-03-08 07:00 in local time
or even 2021-03-07 23:00 in UTC
Note: Combining the fields is not an option unfortunately.
Need to know your time zone to convert. To convert utc to america/los_angeles time zone:
select '2021-03-08 23:00'::timestamp at time zone 'UTC' at time zone 'america/los_angeles'
you can check out below codes:
If you have a timestamp without time zone column and you're storing timestamps as UTC, you need to tell PostgreSQL that, and then tell it to convert it to your local time zone.
select created_at at time zone 'utc' at time zone 'america/los_angeles'
from users;
To be more concise, you can also use the abbreviation for the time zone:
select created_at at time zone 'utc' at time zone 'pst'
from users;
To see the list of time zones PostgreSQL supports:
select * from pg_timezone_names;
SInce the time difference is 8 hours, try
SELECT '2021-03-08'::date + ('23:00'::time + '8 hours'::interval);
If you want this to work with arbitrary time zones, the query becomes more complicated:
SELECT '2021-03-08'::date
+ ((current_date + '23:00'::time)
AT TIME ZONE 'UTC'
AT TIME ZONE 'Asia/Ulaanbaatar'
)::time;

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'

ORA-01857: not a valid time zone

I am trying to convert from UTC time zone to GMT time zone.
I ran this below query and getting ORA error.
select NEW_TIME(SYSDATE, 'UTC', 'GMT') from dual;
And error is
Error starting at line : 1 in command -
select NEW_TIME(SYSDATE, 'UTC', 'GMT') from dual
Error report -
ORA-01857: not a valid time zone
I googled and find that NEW_TIME function is not accepting UTC time zone.
So, Can you please suggest me alternate solution/any way to convert from UTC to GMT?
UTC is also known as GMT, the latter which NEW_TIME already accepts. So, what you are trying to is equivalent to:
SELECT NEW_TIME(SYSDATE, 'GMT', 'GMT')
FROM dual;
The call to NEW_TIME doesn't make any sense of course. Check here for a list of accepted timezone codes.
Use FROM_TZ from convert a timestamp without a time zone to a timestamp with time zone (i.e. UTC) and then use AT TIME ZONE 'GMT' to convert it from the first time zone to the GMT time zone. You'll need to use CAST in various places as FROM_TZ expects a TIMESTAMP rather than a DATE and then you need to cast back to a DATE at the end (assuming you don't want a TIMESTAMP value):
SELECT CAST(
FROM_TZ(
CAST( SYSDATE AS TIMESTAMP ),
'UTC'
)
AT TIME ZONE 'GMT'
AS DATE
) As gmt_time
FROM DUAL
Output:
| GMT_TIME |
| :------------------ |
| 2019-04-10T14:05:37 |
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;

Oracle UTC Time

I'm trying to read an Oracle TIMESTAMP WITH TIMEZONE from a database in UTC-5 (NY) as UTC.
Oracle is driving me crazy:
SELECT
from_tz(MAX(TIMESTAMPWITHTIMEZONE),'UTC'),
SYS_EXTRACT_UTC(MAX(TIMESTAMPWITHTIMEZONE)),
SYS_EXTRACT_UTC(systimestamp),
SYSTIMESTAMP AT TIME ZONE 'UTC'
FROM TABLE
Results:
SYS_EXTRACT_UTC(systimestamp) gives me: 2013-02-20 14:59:04, which is probably right.
SYSTIMESTAMP AT TIME ZONE 'UTC' gives me: 2013-02-20 15:59:04 which is my own local Berlin - whatever
Now I want to have TIMESTAMPWITHTIMEZONE (TIMESTAMP(6)) as UTC
SYS_EXTRACT_UTC(MAX(TIMESTAMPWITHTIMEZONE)) is 2013-02-20 08:55:01
from_tz(MAX(TIMESTAMPWITHTIMEZONE),'UTC') is 2013-02-20 10:55:01
Srly. Oracle. I want UTC.
Which one is the right one? Or is there a better way?
The functions are different:
SYS_EXTRACT_UTC converts a TIMESTAMP WITH TIMEZONE to a TIMESTAMP (with inferred but absent timezone=UTC).
FROM_TZ converts a TIMESTAMP to a TIMESTAMP WITH TIMEZONE
These functions when applied to a single value will in general return a different result:
SQL> SELECT sys_extract_utc(localtimestamp) ext,
2 from_tz(localtimestamp, 'UTC') from_tz
3 FROM dual;
EXT FROM_TZ
--------------------- ------------------------
2013/02/20 15:34:24 2013/02/20 16:34:24 UTC
In the first case the TIMESTAMP is implicitly given the timezone of the server and then transformed into the equivalent timestamp at the UTC timezone. Note that in general you should stay away from implicit conversions.
In the second case there is no computation between timezones: the FROM_TZ function adds the geographical location to a point in time variable.
By the way there is something missing in your example: you can't apply the FROM_TZ function on a variable of type TIMESTAMP WITH TIMEZONE (tested on 9ir2 and 11ir2):
SQL> select from_tz(systimestamp, 'UTC') from dual;
select from_tz(systimestamp, 'UTC') from dual
ORA-00932: inconsistent datatypes:
expected TIMESTAMP got TIMESTAMP WITH TIME ZONE
Edit following comment:
In your case assuming that your column are of time TIMESTAMP, and knowing that they refer to the NY timezone, you could use the AT TIME ZONE expression to convert to UTC:
SQL> SELECT localtimestamp,
2 from_tz(localtimestamp, 'America/New_York') AT TIME ZONE 'UTC' utc
3 FROM dual;
LOCALTIMESTAMP UTC
--------------------- ------------------------
2013/02/20 17:09:09 2013/02/20 22:09:09 UTC
From Oracle 18c you could use TO_UTC_TIMESTAMP_TZ function:
The TO_UTC_TIMESTAMP_TZ function converts any valid ISO 8601 date represented as a string into a TIMESTAMP WITH TIMEZONE, which can optionally be used as input to the SYS_EXTRACT_UTC function.
SELECT TO_UTC_TIMESTAMP_TZ(col_name)
FROM tab_name;