How to convert Local_Timestamp into UTC Timestamp? - sql

The Timestamp is the localtime of the DB2 Server.
The date was stored in the time zone Europe / Berlin.
I would like to convert the then stored date to UTC. Is there a way to calculate this in DB2 Dialect?

Check out the TO_UTC_TIMESTAMP scalar function
values TO_UTC_TIMESTAMP(TIMESTAMP'2019-10-01 00:00:00', 'Europe/Berlin')
Please note the second parameter is case sensitive.
There is also a FROM_UTC_TIMESTAMP if needed.

Your best bet is to use the TIMEZONE function and to use CURRENT TIMEZONE as help for the input.
The function converts date and time from one to another timezone. The register CURRENT TIMEZONE gives you the difference between server timezone and UTC.

As #data_henrik pointed out, CURRENT TIMEZONE is the special register to use, it can be simply subtracted from CURRENT TIMESTAMP e.g.:
db2 "values CURRENT TIMESTAMP - CURRENT TIMEZONE"
1
--------------------------
2019-10-16-11.36.24.025651
1 record(s) selected.

Related

Timestamp string conversion / from_utc_timestamp

I need to convert 2021-10-03 15:10:00.0 as 2021-10-03T15:10:00-04:00
I tried with.
from_utc_timestamp(from_unixtime(unix_timestamp('2021-10-03 15:10:00.0', "yyyy-MM-dd HH:mm:ss.S"),"yyyy-MM-dd'T'HH:mm:ssXXX"),"America/New_York")
I got Null value
Any suggestions please
from_utc_timestamp can accept timestamp or compatible string (yyyy-MM-dd HH:mm:ss.S), or bigint, not this: "yyyy-MM-dd'T'HH:mm:ssXXX"
Hive timestamps are timezoneless. Once you converted from UTC to America/NY, the timezone information is lost, only you know in which timezone it is, having timestamp converted it is already impossible to derive the timezone from it.
You can concatenate with timezone, conversion like this returns what you need but it works for particular date only. In December -05:00 timezone should be usedm not +04:00:
date_format(from_utc_timestamp('2021-10-03 15:10:00.0',"America/New_York"),"yyyy-MM-dd'T'HH:mm:ss+04:00") --This is wrong!!!
From_utc_timestamp is Daylight saving aware. It can be -05:00 or -04:00 depending on the date.
Consider this example, first returns 5, second returns 4:
select (unix_timestamp("2020-01-01 12:00:00.0")-unix_timestamp(from_utc_timestamp("2020-01-01 12:00:00.0","America/New_York")))/60/60
select (unix_timestamp("2020-10-19 12:00:00.0")-unix_timestamp(from_utc_timestamp("2020-10-19 12:00:00.0","America/New_York")))/60/60
So, you can get current time zone corresponding to America/New_York for the same timestamp and concatenate it with converted timestamp:
select concat(date_format(from_utc_timestamp('2021-10-03 15:10:00.0',"America/New_York"),"yyyy-MM-dd'T'HH:mm:ss"),'+0',
--get hrs shift
(unix_timestamp("2021-10-03 15:10:00.0")-unix_timestamp(from_utc_timestamp("2021-10-03 15:10:00.0","America/New_York"))) div 3600,':00')
Result:
2021-10-03T11:10:00+04:00
It should work correctly with different timestamps taking into account daylight saving time for America/New_York.

how to convert TIMESTAMP WITH TIMEZONE to TIMESTAMP but keeping local time in SQL

I wonder how such conversion can be done in SQL, e.g.
2020-07-03 19:47:51.494 America/Los_Angeles => 2020-07-03 19:47:51.494
Note the input data type TIMESTAMP WITH TIMEZONE and the output type is TIMESTAMP.
In particular, I'm using prestosql from https://prestosql.io/.
Per SQL standard, CAST should do that.
In Presto, under default settings, it does not so today.
This is tracked by https://github.com/prestosql/presto/issues/37
However, you can unlock the SQL standard behavior with a session toggle
presto> SET SESSION legacy_timestamp = false;
SET SESSION
presto> SELECT CAST(TIMESTAMP '2020-07-03 19:47:51.494 America/Los_Angeles' AS timestamp);
_col0
-------------------------
2020-07-03 19:47:51.494
Hmmm . . . a brute force way is to convert to a string and then back to a timestamp:
date_parse(format_datetime(datecol, '%Y-%m-%d %H:%i:%s'), '%Y-%m-%d %H:%i:%s')
Note that this changes the meaning of the value in the column. A timestamp with timezone is really a UTC value that is offset for display purposes. I don't recommend doing this in general. But I have had to do similar operations when local times were moved into "timestamp with timezone" values in a database -- but in the wrong timezone.

Convert datetime to UTC beginning of the day - Oracle db

For example:
I've got datetime in Oracle database like: 18/08/21 13:51:23,420460500 (y/m/d) and I want convert to type: 18/08/20 22:00:00,000000000.
Could you please let me know how can I do this?
I've tried SYS_EXTRACT_UTC("MyDate") but it does not work in that case.
From the format of your question, I'm assuming you have a TIMESTAMP value, since Oracle doesn't have a "datetime" data type. You can truncate the time component to midnight (the beginning of the day) with the TRUNC function, though it returns a DATE data type. I'm not sure where the 22:00 in your question is coming from.

Converting timezone from UTC using numbers as opposed to names for timezones in BigQuery SQL?

I know that in BigQuery one can convert a timestamp by DATETIME(timestamp, timezone). But the names of the timezones in SQL are very poorly organized. I was wondering if there is a function or a way to convert from a time in
UTC to some other timezone using a string of number like "+00:04" or "4" where the number would indicate the amount of hours the timezone is ahead or behind the UTC time.
Thank you!
You can specify a timezone by supplying its UTC offset using the following format:
(+|-)H[H][:M[M]]
For example:
-07:00
SELECT CURRENT_DATETIME('-07:00'), DATETIME(CURRENT_TIMESTAMP(), '-07:00')

Convert date value to PST for comparison:Oracle

I have 2 questions:
I want to compare a field whose data type is "Date" against a given date. The DB is oracle and being a mysql guy I'm finding it difficult to come up with simple queries.
The field("date_closed") stores date in UTC format (24-Aug-2011 18:55:11 for example) and I want to convert it to PST for comparison.
I tried this query but it returns some extra rows in the data set(obviously):
select * from table1 where trunc(date_closed)=to_date('2011-08-24','yyyy-mm-dd')
How do I covert to PST format before comparison?
In the same query how do I compare "date_closed" against the current date?
You need the NEW_TIME function
Dates don't include timezone in Oracle, and are assumed to be in the database timezone (which may by UTC but probably isn't). You should look at the TIMESTAMP WITH TIMEZONE data types.
Also, bear in mind that if you are comparing to the current date - I assume that you want to strip off the timestamp and compare only the day.
So, if new_time(date_closed,'GMT','PST') translates the date , your where clause will be comparing something like
trunc(new_Time(date_closed,'GMT','PST')) = trunc(sysdate)
to get all records with date_closed on the current day in PST.