如何在 Snowflake 中将 UNIX 纪元日转换为 DATE?

How to convert UNIX epoch days to DATE in Snowflake?

我有一些值对应于自大纪元以来的天数,并希望将它们存储在 Snowflake 的 DATE 列中。

示例: 33057

对应于 2060 年 7 月 4 日星期日

如果有人能为我指出正确的 Snowflake 文档(如果开箱即用),那将非常有帮助

我认为这会起作用:

select dateadd(day, <your number> - 33057, '2060-07-04')

因此,对于“33057”的具体转换,这将是:

select dateadd(day, 33057 - 33057, '2060-07-04')

但它应该适用于任何整数。

TO_TIMESTAMP_x(numeric_expr) 就是您要找的。如果你只想要日期部分,我会通过 ::DATE

截断

numeric_expr A number of seconds (if scale = 0 or is absent) or fractions of a second (e.g. milliseconds or nanoseconds) since the start of the Unix epoch (1970-01-01 00:00:00 UTC). If a non-integer decimal expression is input, the scale of the result is inherited.

如果你有毫秒,你可以使用TO_TIMESTAMP_x(numeric_expr, scale)形式

select to_timestamp(12334567) as t_from_s
  ,to_timestamp(12334567000, 3) as t_from_ms;

给予

T_FROM_S                   T_FROM_MS
1970-05-23 18:16:07.000    1970-05-23 18:16:07.000

我们对所有用途都使用 _NTZ 变体,因为我们拥有来自服务器的所有 UTC 数据。