I need to change a UTC timestamp to 'US/Eastern' timestamp without changing the date and time - essentially update only the timezone information and later convert that to a different timezone.
For example (what I need):
'2021-06-09 19:00:36.000000' UTC --> '2021-06-09 19:00:36.000000' US/Eastern
Then I need to convert that to 'America/New_York'.
'2021-06-09 19:00:36.000000' US/Eastern --> '2021-06-09 16:00:36.000000' America/Los Angeles
When I try the query below, it's not giving me the correct results, since it is converting from UTC to America/Los Angeles. When it should be US/Eastern to America/Los Angeles.
SELECT id
, date_utc
, CAST(date_utc AT TIME ZONE 'America/Los Angeles') AS date_la
FROM call_records
I'm not sure if this will work for Athena, as it's based on a very old version of Presto/Trino.
In recent versions of Trino (formerly known as PrestoSQL), you can do this:
Cast the timestamp with time zone to timestamp to remove the timezone part.
Then, use with_timezone to reinterpret the resulting timestamp in US/Eastern.
Finally, use AT TIME ZONE to change the time zone of the resulting timestamp with time zone while preserving the instant.
Take a look at the example below:
trino:tiny> WITH t(ts) AS (VALUES TIMESTAMP '2021-06-09 19:00:36.000000 UTC')
-> SELECT with_timezone(cast(ts as timestamp(6)), 'US/Eastern') AT TIME ZONE 'America/Los_Angeles'
-> FROM t;
_col0
------------------------------------------------
2021-06-09 16:00:36.000000 America/Los_Angeles
(1 row)
Related
I am trying to insert the timestamp with timezone value into the s3 table from the parquet file but could not able to cast milliseconds and timezone.
Source data : 2022-03-12 13:21:38.688000 +00:00
Cast syntax used : cast(to_timestamp(timestamp_col,'yyyy-MM-dd HH:mm:ss') as timestamp) EXECUTION_TS /
cast(from_unixtime(unix_timestamp(substr(timestamp_col,'yyyy-MM-dd HH:mm:ss')) as timestamp) EXECUTION_TS
op i got: 2022-03-12T13:21:38.000+0000
i used .ms TZ/.SSSSS TZD to get milliseconds information but it's populating as null in output. can please tell me how we can retrieve milliseconds and time zone information.
I have difficulties in converting this timestamp string 2020-09-08T15:30:00+00:00 to a correct UTC time:
If I do this:
select to_timestamp('2020-09-08T15:30:00+00:00', 'YYYY-MM-DD"T"HH24:MI:SS');
I get 2020-09-08 15:30:00.000000 -04:00, which is on a wrong timezone.
How can I parse the +00:00 part of the string? I tried TZ/OF based on AWS document but they are not allowed to be added:
[0A000][500310] [Amazon](500310) Invalid operation: "TZ"/"tz" not supported;
, while I'm doing this: select to_timestamp('2020-09-08T15:30:00+00:00', 'YYYY-MM-DD"T"HH24:MI:SS+TZ');
Not sure why you are trying to convert using to_timestamp, because in your example its already UTC/GMT only.
I have used this way in past, I hope it should work for you as well.
#below is something saved in IST(+5:30) to GMT
SELECT CONVERT_TIMEZONE('GMT','2020-09-08 15:30:00+05:30');
Similarly, it could be converted to US/Newyork timezone.
SELECT CONVERT_TIMEZONE('America/New_York','2020-09-08 15:30:00+05:30') ;
I'm currently exporting data from Dynamics. There are a lot of date fields I need to export. By default they are all UTC timezone. I want to report on New Zealand timezone. I tried to approaches in Power Query:
1) use DateTimeZone.ToLocal: =Table.TransformColumns(#"dataset", {{"**UTC Date**", **DateTimeZone.ToLocal**, type datetimezone}})
2) use a specific timezone ("en-NZ"): =Table.TransformColumnTypes(#"dataset", {{"UTC Date", type datetime}}, **"en-NZ"**)
Both approaches work OK in Power BI desktop report, However once I published to Power BI service and after several refreshes (initially it was NZ time), the time turn back to UTC time.
I don't want to create extra columns in DAX and really want to try use Power Query. Is there any way to work it out?
I have faced the same issue a while back and came up with the following solution.
You can simply use a combination of RemoveZone and Addzone power query function to achieve this. Example below shows how to get Indian Standard Time(IST = +5:30)
= DateTime.AddZone(DateTimeZone.RemoveZone(DateTimeZone.UtcNow()),5,30)
Note that I used DateTimeZone.UtcNow() to always get the Universal standard DateTime and then convert this to the Indian time. You can use your own timezone values
Add the above code in place of DateTimeZone.ToLocal in your code.
Documentation to DateTime.AddZone
Power BI:
Use this Power Query function to convert times from UTC to Local Time, here its mountain time, but you can set to Whatever New Zealand Time is
let
ConvertDateColumnstoMountainTime = (sourcetable as table) as table =>
let
TargetColumnList = Table.ColumnsOfType(sourcetable, {type datetime, type nullable datetime, type datetimezone, type nullable datetimezone}),
AdjustTimeZones = Table.TransformColumns(sourcetable, List.Transform(TargetColumnList,
(name) => {name, (date) => if date <> null
then DateTime.From(DateTimeZone.RemoveZone(date) + #duration(0,CalculateUTCOffset(date,null),0,0))
else null})),
//Above returns type as text. Need it explicitly as Date.
AdjustColumnTypes = Table.TransformColumnTypes(AdjustTimeZones,List.Transform(TargetColumnList,
(name) => {name, type datetime}))
in AdjustColumnTypes
in ConvertDateColumnstoMountainTime
The database (SQLite) has a field of type REAL with the values of the form (42153.659595).
How to translate this value in the form "dd.MM.yy HH:mm:ss" if 42153.659595 = 29.05.2015 15:49:49 ?
You can be explicit about what calendar system you require: http://www.sqlite.org/lang_datefunc.html
SELECT julianday('now') - julianday('1776-07-04');
In principle just don't "parse" (you mean: interpret raw representation). Use Sqlite API/builtin SQL functions to do it for you
In the interest of information:
The date and time functions use a subset of IS0-8601 date and time formats.
The datetime() function returns "YYYY-MM-DD HH:MM:SS". The julianday() function returns the Julian day - the number of days since noon in Greenwich on November 24, 4714 B.C. (Proleptic Gregorian calendar)
The date() function returns the date in this format: YYYY-MM-DD. The time() function returns the time as HH:MM:SS.
The correct value is achieved using the API/builtin SQL functions (42153.659595 - value from database):
SELECT datetime(julianday(42153.659595, 'localtime') + 2415018.29167) AS DT;
Output:
DT |
2015-05-29 15:49:49|
Constant 2415018.29167 was selected manually and query:
SELECT datetime (2415018.29167);
returns the current Greenwich Mean Time.
I work with a third-party application and documentation on the database is missing. Perhaps this strange decision, but it works. Thank you all for answers.
We have a staging table that's used to load raw data from our suppliers.
One column is used to capture a time-stamp but its data-type is varchar(265). Data's dirty: about 40% of the time, there is garbage data, otherwise time-stamp data like this
2011/11/15 20:58:48.041
I have to create a report that filters some dates/timestamps out that column but where I try to cast it, I get an error:
db2 => select cast(loadedon as timestamp) from automation
1
--------------------------
SQL0180N The syntax of the string representation of a datetime value is incorrect. SQLSTATE=22007
What do I need to do in order to parse/cast the timestamp string?
The string format for a DB2 timestamp is either:
'2002-10-20-12.00.00.000000'
or
'2002-10-20 12:00:00'
You have to get your date string in either of these formats.
Also DB2 runs on a 24 hour clock even though the output sometimes uses a 12 hour clock (AM / PM)
So '2002-10-20 14:49:50' For 2:49:50 PM
Or '2002-10-20 00:00:00' For midnight. Output would be 12:00:00 AM
It seems you have a lot of garbage data, so firt of all you should check if the data is a valid timestamp in the format you expect ('2011/11/15 20:58:48.041'). We could use a simple solution - just replace all digits with '0' and check the result format:
TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
If the format is the expected one, you should convert to DB2 timestamp. In DB2 for iSeries there is a build-in function since V6R1 TIMESTAMP_FORMAT. In your case it will look like that:
TIMESTAMP_FORMAT('2011/11/15 20:58:48.041','YYYY/MM/DD HH24:MI:SS.NNNNNN')
So the solution query combined should look something like that:
SELECT
CASE
WHEN TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
THEN TIMESTAMP_FORMAT(timestamp_column,'YYYY/MM/DD HH24:MI:SS.NNNNNN')
ELSE NULL
END
FROM
your_table_with_bad_data
EDIT
I just saw your comment that provider agreed to clean the data. You could use the solution provided to speed up the process and clean the data by yourself:
ALTER your_table_with_bad_data ADD COLUMN clean_timestamp TIMESTAMP DEFAULT NULL;
UPDATE your_table_with_bad_data
SET clean_timestamp =
CASE
WHEN TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
THEN TIMESTAMP_FORMAT(timestamp_column,'YYYY/MM/DD HH24:MI:SS.NNNNNN')
ELSE NULL
END;