[英]How can I convert a TIMESTAMP_NTZ what I know is in UTC to TIMESTAMP_TZ in Snowflake SQL?
I have a column of type TIMESTAMP_NTZ
and it contains timestamp that I know are on UTC timezone.我有一个
TIMESTAMP_NTZ
类型的列,它包含我知道在 UTC 时区的时间戳。 Mainly I want to convert to TIMESTAMP_TZ
because I have other columns that are TIMESTAMP_TZ
and I want to be able to do operations involving both.主要是我想转换为
TIMESTAMP_TZ
因为我有其他列是TIMESTAMP_TZ
并且我希望能够进行涉及两者的操作。
I've tried to generate a TIMESTAMP_TZ
in the following ways:我尝试通过以下方式生成
TIMESTAMP_TZ
:
SELECT
'2019-10-24 12:07:24.000'::timestamp_ntz as orig -- a literal timestamp_ntz
,convert_timezone('UTC', 'UTC', orig) -- the 3-args version of convert_timezone will always return a TIMESTAMP_NTZ
,convert_timezone('UTC', orig) -- the 2-args assumes that orig timezones is session's current timezone
;
Just to be clear I want to convert 2019-10-24 12:07:24.000
to 2019-10-24 12:07:24.000 +0000
为了清楚起见,我想将
2019-10-24 12:07:24.000
转换为2019-10-24 12:07:24.000 +0000
Is there any function or operator in Snowflake that allows me to say treat this TIMESTAMP_NTZ
as TIMESTAMP_TZ
in timezone X? Snowflake 中是否有任何 function 或运算符允许我说将此
TIMESTAMP_NTZ
视为时区 X 中的TIMESTAMP_TZ
?
There are two ugly hacks that you can use您可以使用两种丑陋的技巧
TIMESTAMP_TZ
using all the components from the source TIMESTAMP_NTZ
and provide the timezone as the last parameter. TIMESTAMP_NTZ
中的所有组件构造一个新的TIMESTAMP_TZ
,并提供时区作为最后一个参数。 It's very verbose.TIMESTAMP_NTZ
as a string / VARCHAR
add the timezone string at the end of it and reparse the whole thing as a TIMESTAMP_TZ
. TIMESTAMP_NTZ
序列化为字符串 / VARCHAR
在其末尾添加时区字符串并将整个内容重新解析为TIMESTAMP_TZ
。select
'2019-10-24 12:07:24.567'::timestamp_ntz as orig
,TIMESTAMP_TZ_FROM_PARTS( year(orig), month(orig),day(orig), hour(orig), minute(orig), second(orig) , date_part(nanosecond, orig), 'UTC' )
,TO_TIMESTAMP_TZ(orig::varchar || ' +0000')
;
--
2019-10-24 12:07:24.567 -- TIMESTAMP_NTZ (UTC)
2019-10-24 12:07:24.567 +0000 -- TIMESTAMP_TZ (UTC)
2019-10-24 12:07:24.567 +0000 -- TIMESTAMP_TZ (UTC)
You should be able to just suffix a "Z" onto the end of the original TIMESTAMP_NTZ and then convert it to a TIMESTAMP_TZ like the following:您应该能够在原始 TIMESTAMP_NTZ 的末尾添加一个“Z”后缀,然后将其转换为 TIMESTAMP_TZ,如下所示:
-- Set timezone to something which isn't UTC
alter session set TIMEZONE = 'America/Los_Angeles';
-- Make sure the timestamp TZ output format is as you'd expect
alter session set TIMESTAMP_TZ_OUTPUT_FORMAT = "YYYY-MM-DD HH24:MI:SS.FF3 TZHTZM";
select
'2019-10-24 12:07:24.567'::timestamp_ntz as orig, -- Outputs "2019-10-24 12:07:24.567000000"
(orig::varchar||'Z')::timestamp_tz timestamp_with_tz --Outputs "2019-10-24 12:07:24.567 +0000"
;
This works if you know the local timezone of your account, user, or session.如果您知道您的帐户、用户或 session 的本地时区,则此方法有效。 For me, it's 'America/New_York':
对我来说,它是“美国/纽约”:
SELECT current_timestamp::timestamp_ntz as orig,
convert_timezone('UTC',convert_timezone('UTC','America/New_York',orig)::timestamp_tz);
Also, if this is a 1-time conversion, you could run:此外,如果这是 1 次转换,您可以运行:
ALTER SESSION SET TIMEZONE = 'UTC'
This would temporarily display values as UTC and you could convert them to _tz with:这会将值暂时显示为 UTC,您可以使用以下命令将它们转换为 _tz:
SELECT convert_timezone('UTC', orig)
I would suggest setting the session timezone to UTC first.我建议首先将 session 时区设置为 UTC。
ALTER SESSION SET TIMEZONE = 'UTC';
If you can't do that, and you don't know what the session timezone is, you could calculate that as part of your query:如果您不能这样做,并且您不知道 session 时区是什么,您可以将其计算为查询的一部分:
SELECT
DATEDIFF(minute, CONVERT_TIMEZONE('UTC', CURRENT_TIMESTAMP)::timestamp_ntz, CURRENT_TIMESTAMP) AS utc_offset,
'2019-10-24 12:07:24.567'::timestamp_ntz AS orig,
CONVERT_TIMEZONE('UTC', DATEADD(minute, utc_offset, orig)) as tz_time;
This avoids any string manipulation (which I strongly recommend to avoid).这避免了任何字符串操作(我强烈建议避免)。
How about converting to local time zone and using convert_timezone to get the timezone如何转换为本地时区并使用 convert_timezone 获取时区
SELECT '2019-10-24 12:07:24.000'::timestamp_ntz::timestamp_ltz as orig,convert_timezone('UTC', orig); 0/p= 2019-10-24 19:07:24.000 +0000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.