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.
Related
I'm new to Big Query,
I want to convert the following date-time to UTC timezone.
DateTime format "2021-07-03T23:59:00+04:00[Asia/Dubai]" to UTC.
Thank you.
You just need to convert it to TIMESTAMP() without specifying any timezone (since it automatically sets the datetime at UTC) and then convert it back to DATETIME():
SELECT
your_datetime,
DATETIME(TIMESTAMP(your_datetime)) AS datetime_utc
FROM
<your_table>
OUTPUT:
your_datetime datetime_utc
------------------------- -------------------
2021-07-03T23:59:00+04:00 2021-07-03T19:59:00
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.
In my query I need to return only those records that are greater than another date, returned by the function. The field I am comparing is a timestamp and the date function returns is a of type DATE.
My query:
SELECT * FROM TABLE1
WHERE MY_TIMESTAMP > MyFunction1('Test Date');
when I am comparing two values where only time differs slightly - in munutes or seconds, the comparison does not work.
For example when
MY_TIMESTAMP=11/27/2018 12:15:42.000000 PM -05:00
and
MyFunction1('Test Date') returns 11/27/2018 12:22:00 PM
no record should be returned from TABLE1 for that ID but the record does get returned. What can I do to make the comparison more granular?
From Datetime and Interval Arithmetic
Oracle Database performs all timestamp arithmetic in UTC time. For
TIMESTAMP WITH LOCAL TIME ZONE data, Oracle Database converts the
datetime value from the database time zone to UTC and converts back to
the database time zone after performing the arithmetic. For TIMESTAMP WITH TIME ZONE data, the datetime value is always in UTC, so no
conversion is necessary.
If you provide a DATE or TIMESTAMP value (i.e. value without any time zone information) then Oracle attaches the SESSIONTIMEZONE to this value and then it does the comparison based on UTC.
Attaching SESSIONTIMEZONE to input value might be correct or not, it depends on your current session and content of MyFunction1
I could appreciate a second pair of eyes on my Postgres syntax.
Database stores timestamp in UTC. I'm trying to convert from UTC to Eastern Daylight Time EDT, but the output is not accurate.
Here's my syntax:
SELECT
to_char(((timestamp AT TIME ZONE 'UTC') AT TIME ZONE 'EDT'), 'MM/DD/YYYY HH24:MI')
FROM table_name
Record TimeStamp:
09/10/2016 12:00
Query Output:
09/10/2016 16:00
Desired Output:
09/10/2016 08:00
Thanks for your assistance.
Saying AT TIMEZONE twice is redundant since it will just convert from whatever the current timezone (which you suggest is UTC) to UTC then to EDT.
The fact that you feel the need to convert it to UTC tells me you're not storing it as a TIMESTAMP WITH TIMEZONE. Check if this is the case. If it is, that's likely your problem. From the docs:
If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's timezone parameter, and is converted to UTC using the offset for the timezone zone.
Basically, if you don't specify, it assumes it's from your current timezone by default, not UTC. It's possible you entered a UTC timestamp and it assumed it was EDT.
I have a date as a timestamp . I have to replace it every day on the date of beginning of 7:00:00 hours a day from the date of timestamp for example . 11-07-2016 7:00:00
For example, I have a date after changing 837072216 837241200 it will need a scalar function .
Your question is very unclear. In fact, there is no actual question...
Never-the-less, you appear to be asking about a conversion from timestamp to a date or time. This is not possible; despite its name, timestamp does not represent a date(time). It is used for versioning of rows, and it's deprecated and replaced by rowversion. Source.