简体   繁体   English

Presto SQL:TO_UNIXTIME

[英]Presto SQL: TO_UNIXTIME

I want to convert a readable timestamp to UNIX time.我想将可读时间戳转换为 UNIX 时间。

For example: I want to convert 2018-08-24 18:42:16 to 1535136136000 .例如:我想将2018-08-24 18:42:16转换为1535136136000

Here is my syntax:这是我的语法:

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

My error is:我的错误是:

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

You need to wrap the varchar in a CAST to timestamp:您需要将 varchar 包装在 CAST 到时间戳中:

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

If your timestamp value doesn't have fraction of second (or you are not interested in it), you can cast to bigint to have integral result:如果您的时间戳值没有秒的小数部分(或者您对它不感兴趣),您可以转换为 bigint 以获得整数结果:

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

If your readable timestamp value is a string in different format than the above, you would need to use date_parse or parse_datetime for the conversion.如果您的可读时间戳值是与上述格式不同的字符串,则需要使用date_parseparse_datetime进行转换。 See https://trino.io/docs/current/functions/datetime.html for more information.有关详细信息,请参阅https://trino.io/docs/current/functions/datetime.html

Note: when dealing with timestamp values, please keep in mind that: https://github.com/trinodb/trino/issues/37注意:在处理时间戳值时,请记住: https ://github.com/trinodb/trino/issues/37

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

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