【问题标题】:Presto SQL: TO_UNIXTIMEPresto SQL:TO_UNIXTIME
【发布时间】:2018-08-24 18:49:37
【问题描述】:

我想将可读的时间戳转换为 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)

【问题讨论】:

    标签: amazon-web-services presto amazon-athena trino


    【解决方案1】:

    您需要将 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

    【讨论】:

    • 谢谢@PiotrFindeisen!它没有给我错误,但它返回`1.535092936E9`,我想要的是1535136136000
    • TO_UNIXTIME 返回double (因为时间戳具有毫秒精度),因此您需要转换为 bigint。已更新。
    猜你喜欢
    • 2018-11-11
    • 2020-08-24
    • 2018-02-16
    • 2019-01-21
    • 2019-02-15
    • 2016-04-02
    • 2017-06-29
    • 2013-12-01
    • 2019-12-16
    相关资源
    最近更新 更多