简体   繁体   English

在 Snowflake 中将日期转换为数字时间戳

[英]Converting a date to a numeric timestamp in Snowflake

I have a query wherein one of the columns is a DATE type.我有一个查询,其中一列是DATE类型。 I'm trying to convert that to the nanosecond representation of the timestamp associated with the date:我正在尝试将其转换为与日期关联的时间戳的纳秒表示形式:

Input输入 Output Output
2022-07-15 2022-07-15 1657843200000000000 1657843200000000000
2022-07-18 2022-07-18 1658102400000000000 1658102400000000000
2022-07-19 2022-07-19 1658188800000000000 1658188800000000000

I can get a timestamp from a date by doing this:我可以通过这样做从日期获取时间戳:

SELECT TRY_TO_TIMESTAMP(TO_VARCHAR($1))
FROM (
    SELECT DATE($1) FROM VALUES ('2022-07-15'), ('2022-07-18'), ('2022-07-19'))

but using TRY_TO_NUMERIC doesn't work so I'm not sure what to do here.但是使用TRY_TO_NUMERIC不起作用,所以我不确定在这里做什么。

You can use the datediff function to return the nanoseconds since the start of the Unix epoch, which is 1970-01-01:您可以使用 datediff function 返回自 Unix 纪元开始以来的纳秒数,即 1970-01-01:

with source_data as
(
    select 
    COLUMN1::date as INPUT
    from (values
    ('2022-07-15'),
    ('2022-07-18'),
    ('2022-07-19')
    )
)
select   INPUT
        ,datediff(nanoseconds, '1970-01-01'::date, INPUT) as OUTPUT
from SOURCE_DATA
;
INPUT输入 OUTPUT OUTPUT
2022-07-15 2022-07-15 1657843200000000000 1657843200000000000
2022-07-18 2022-07-18 1658102400000000000 1658102400000000000
2022-07-19 2022-07-19 1658188800000000000 1658188800000000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM