简体   繁体   English

如何将我所知道的 UTC TIMESTAMP_NTZ 转换为 Snowflake SQL 中的 TIMESTAMP_TZ?

[英]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_FROM_PARTS : Construct a new TIMESTAMP_TZ using all the components from the source TIMESTAMP_NTZ and provide the timezone as the last parameter. TIMESTAMP_TZ_FROM_PARTS :使用源TIMESTAMP_NTZ中的所有组件构造一个新的TIMESTAMP_TZ ,并提供时区作为最后一个参数。 It's very verbose.它非常冗长。 See the example below.请参见下面的示例。
  • TO_TIMESTAMP_TZ and CONCAT : Serialize the TIMESTAMP_NTZ as a string / VARCHAR add the timezone string at the end of it and reparse the whole thing as a TIMESTAMP_TZ . TO_TIMESTAMP_TZCONCAT :将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.

相关问题 在 Snowflake SELECT 中将 TIMESTAMP_TZ 转换为 TIMESTAMP_NTZ - Convert TIMESTAMP_TZ to TIMESTAMP_NTZ in Snowflake SELECT 如何将 Snowflake 中的数组或字符串转换为 Timestamp_NTZ - How to Convert Array or String in Snowflake to Timestamp_NTZ 雪花 SQL 错误 - function '-' 的参数类型无效:(TIMESTAMP_NTZ(9),TIMESTAMP_NTZ(9)) - Snowflake SQL error - Invalid argument types for function '-': (TIMESTAMP_NTZ(9), TIMESTAMP_NTZ(9)) 雪花无法将变量值“{TIMESTAMP VALUE}”转换为 TIMESTAMP_NTZ - Snowflake Failed to cast variant value "{TIMESTAMP VALUE}" to TIMESTAMP_NTZ 如何将DB2 TIMESTAMP转换为UTC毫秒? - How can I convert DB2 TIMESTAMP to UTC milliseconds? 如何在iSeries上的SQL400中获得UTC时间戳? - How can I get a UTC timestamp in SQL400 on iSeries? 如何在Oracle SQL上比较转换为UTC的当前时间戳和转换为UTC的另一个时间戳? - How do I compare the current timestamp converted to UTC to another timestamp converted to UTC on Oracle SQL? 如何将字符串季度年转换为 Databricks SQL 中的时间戳 - How can I convert a string quarter year to a timestamp in Databricks SQL 如何在 SQL 服务器中将 bigint(UNIX 时间戳)转换为日期时间? - How can I convert bigint (UNIX timestamp) to datetime in SQL Server? 如何将 Local_Timestamp 转换为 UTC 时间戳? - How to convert Local_Timestamp into UTC Timestamp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM