急板 SQL:TO_UNIXTIME

Presto SQL: TO_UNIXTIME

我想将可读时间戳转换为 UNIX 时间。

例如:我想将2018-08-24 18:42:16转换为1535136136000

这是我的语法:

    TO_UNIXTIME('2018-08-24 06:42:16') new_year_ut

我的错误是:

   SYNTAX_ERROR: line 1:77: Unexpected parameters (varchar(19)) for function to_unixtime. Expected: to_unixtime(timestamp) , to_unixtime(timestamp with time zone)

您需要将 varchar 包装在 CAST 到时间戳中:

to_unixtime(CAST('2018-08-24 06:42:16' AS timestamp)) -- note: returns a double

如果您的时间戳值没有秒的小数部分(或者您对此不感兴趣),您可以转换为 bigint 以获得整数结果:

CAST(to_unixtime(CAST('2018-08-24 06:42:16' AS timestamp)) AS BIGINT)

如果您的可读时间戳值是与上述格式不同的字符串,则需要使用 date_parseparse_datetime 进行转换。有关详细信息,请参阅 https://trino.io/docs/current/functions/datetime.html

注意:在处理时间戳值时,请记住:https://github.com/trinodb/trino/issues/37