Convert dates to UTC in DB2 - sql

I need to convert dates in a DB2 Database to UTC values.
Usually the TO_UTC_TIMESTAMP function would be the way to do this, but this method seems to only work if the source time-zone is known. The problem I'm facing is that I need an SQL script, that converts the existing dates from the current time-zone of the database to UTC, without hard-coding the current time-zone.
What I already tried is the following SQL:
-- assuming there is a table called 'test' with a column 'col1' of the type timestamp
SELECT col1 AS my_time_zone, TO_UTC_TIMESTAMP(col1, CURRENT TIMEZONE) AS utc FROM test;
This does not convert the date value, because CURRENT TIMEZONE returns an integer value (in my case 10000 for UTC+01:00). This integer value seems to be converted to a VARCHAR, that cannot be interpreted by the TO_UTC_TIMESTAMP function.
From the DB2 documentation:
timezone-expression
[...] If the expression is not a VARCHAR, it is cast to VARCHAR before the function is evaluated.
[...] If the timezone-expression returns a value that is not a time zone in the IANA time zone database, then the value of expression is returned without being adjusted.
So my question is: Is there a way to get the current time-zone from a DB2 database, in a format that the TO_UTC_TIMESTAMP function can use? Or is there any other way to convert dates from the current time-zone to UTC?

That would be simply col1 - current timezone, given that col1 has the timestamp data type.

Related

Multiple to_timestamp() transformation in SELECT

I am currently having a varchar tm column, that stores timestamps such as: '15.11.2021 11:07:27'
The datestyle is currently set to ISO,MDY
How can I transform that varchar using SELECT value in order to preserve its format?
If I use to_timestamp(tm, 'DD.MM.YYYY HH24:MI:SS')::timestamp without time zone I still get it in other format 2021-11-15 11:07:27
I also tried to do double to_timestamps, but then an error function to_timestamp(timestamp with time zone, unknown) does not exist appears.
So the question is: Is there any way to convert from varchar type "15.11.2021 11:07:27" to timestamp type 15.11.2021 11:07:27 using select statement?
DbFiddle
PS. Even though I used it in DBFiddle, I cant change datestyle on the target server
Postgres version 13.5
EDIT: also, if I use cast(tm as timestamp) I receive date/time field value out of range: "15.11.2021 11:07:27" error.
You could try using:
CONVERT(varchar, timestamp, 103) AS YOUR_VALUE
This way you would convert timestamp without changing its original form.

correct type for SQL snowflake date

I am using an SQL Script to parse a json into a table using dbt. One of the cols had this date value: '2022-02-09T20:28:59+0000'. What would be the correct way to define iso date's data type in Snowflake?
Currently, I just used the date type like this in my dbt sql script:
JSON_DATA:"situation_date"::date AS MY_DATE
but clearly, dateisn't the correct one because later when I test it using select * , I get this error:
SQL Error [100040] [22007]: Date '2022-02-09T20:28:59+0000' is not recognized
so I need to know which Snowflake date data type or datetime type suits the best with this one
Correct pulling the "date from JSON" so not so clear cut:
SELECT
'{"date":"2022-02-09T20:28:59+0000"}' as json_str
,parse_json(json_str) as json
,json:date as data_from_json
,TRY_TO_TIMESTAMP_NTZ(data_from_json, 'YYYY-MM-DDTHH:MI:SS+0000') as date_1
,TRY_TO_TIMESTAMP_NTZ(substr(data_from_json,1,19), 'YYYY-MM-DDTHH:MI:SS') as date_2
;
gives the error:
Function TRY_CAST cannot be used with arguments of types VARIANT and TIMESTAMP_NTZ(9)
Because the type of data_from_json as VARIANT and the TO_DATE/TO_TIMESTAMP function expect TEXT so we need to cast to that
SELECT
'{"date":"2022-02-09T20:28:59+0000"}' as json_str
,parse_json(json_str) as json
,json:date as data_from_json
,TRY_TO_TIMESTAMP_NTZ(data_from_json::text, 'YYYY-MM-DDTHH:MI:SS+0000') as date_1
,TRY_TO_TIMESTAMP_NTZ(substr(data_from_json::text,1,19), 'YYYY-MM-DDTHH:MI:SS') as date_2
;
If all your timezones are always +0000 you can just put that in the parse format (like example date_1), OR you can truncate that part off (like example date_2)
gives:
JSON_STR
JSON
DATA_FROM_JSON
DATE_1
DATE_2
{"date":"2022-02-09T20:28:59+0000"}
{ "date": "2022-02-09T20:28:59+0000" }
"2022-02-09T20:28:59+0000"
2022-02-09 20:28:59.000
2022-02-09 20:28:59.000
Using TRY_TO_TIMESTAMP:
SELECT TRY_TO_TIMESTAMP(JSON_DATA:"situation_date", 'format_here')
FROM tab;
so I need to know which Snowflake date data type or datetime type suits the best with this one
TIMESTAMP_INPUT_FORMAT
The specific input could be set up on ACCOUNT/USER/SESSION level.
AUTO Detection of Integer-stored Date, Time, and Timestamp Values
Avoid using AUTO format if there is any chance for ambiguous results. Instead, specify an explicit format string by:
Setting TIMESTAMP_INPUT_FORMAT and other session parameters for dates, timestamps, and times. See Session Parameters for Dates, Times, and Timestamps (in this topic).
I think ::TIMESTAMP should work for this. So JSON_DATA:"situation_date"::TIMESTAMP if you need to go just to date after, you could then to ::Date or to_Date()
After some testing, it seems to me you have 2 options.
Either you can get rid of the +0000 at the end:
left(column_date, len(column_date)-5)::timestamp
or use the function try_to_timestamp with format:
try_to_timestamp('2022-02-09T20:28:59+0000','YYYY-MM-DD"T"HH24:MI:SS+TZHTZM')
TZH and TZM both are TimeZone Offset Hours and Minutes

postgresql - datetime: does postgresql have datetime type like in mysql?

I know in mysql there is datetime type but couldn't find couterpart in postgresql. I know there is timestamp type in postgresql but that's a timestamp instead of datetime.
Thanks
A MySQL DATETIME is equivalent to a PostgreSQL TIMESTAMP.
Both store a date+time value in an undefined time line. They assume the time offset or time zone is of no interest, or is defined by the application.

Is there a native technique in PostgreSQL to force "timestamp without time zone" to not include milliseconds?

I am using PostgreSQL 9.6.17. (Migrating from MySQL)
A java program writes dates inside a table. The date formats is the following:
2019-01-01 09:00:00
But it can also be 2019-01-01 09:00:00.00 or 2019-01-01 09:00:00.000 when inserted in the database, which messes up my date management in my program when retrieved.
On insertion, I would like all the date to have the very same format: 2019-01-01 09:00:00. The datatype used by the column is timestamp without a time zone.
How can I tell postgresql to not input milliseconds in timestamp without timezone via configuration or SQL query ?
This data types doc does not provide any information about that.
Quote from the manual
time, timestamp, and interval accept an optional precision value p which specifies the number of fractional digits retained in the seconds field. By default, there is no explicit bound on precision. The allowed range of p is from 0 to 6
So just define your column as timestamp(0), e.g.:
create table foo
(
some_timestamp timestamp(0)
);
If you have an existing table with data, you can simply ALTER the column:
alter table some_table
alter column some_timestamp type timestamp(0);
If you now insert a timestamp with milliseconds, the value will be rounded to remove the milliseconds.
Note that technically you still have milliseconds in the stored value, but they are always set to 0
You can cast:
mytimestamptz::timestamp(0)
This will round the result to the nearest second. If you want to truncate instead:
date_trunc('second', mytimestamp)
Retrieve as a timestamp and in the application querying the database, manage the precision however you want. eg. via JDBC you'll get a Java LocalDateTime object, in Python you'll get a datetime object.
If you want to retrieve timestamps as strings, there are lots of formatting options available
SELECT to_char(when, 'YYYY-MM-DD HH24:MI:SS') FROM mytable
Drop any milliseconds on input by specifying the precision option to your timestamp type:
CREATE TABLE mytable (..., when TIMESTAMP(0));

How to convert unix/epoch timestamp into date string in Apache Phoenix SQL

We have created Phoenix views on top of Hbase tables and querying the data. One of the the columns holds epoch timestamp data and we need to convert it into a valid date format, couldn't find any appropriate functions, any help much appreciated.
If type of "the column holds epoch timestamp data" is INTEGER or BIGINT, you can use:
CAST("epoch_time" AS TIMESTAMP)
if its type is VARCHAR, you should first convert value to number through TO_NUMBER()
built-in function, i.e.
CAST(TO_NUMBER("epoch_time") AS TIMESTAMP)