繁体   English   中英

Postgres; select 整数表示使用“2021-12-01 00:00:00”和“2021-12-01 23:59:59”之间的 to_timestamp 查询的日期和时间

[英]Postgres; select integers representing date and time query using to_timestamp between '2021-12-01 00:00:00' and '2021-12-01 23:59:59'

我有包含 unix 时间戳的列 - 表示自纪元以来的秒数的整数。 它们看起来像这样: 1638888715 我正在使用to_timestamp() function 轻松将此 int 转换为时间戳,并到达 output,如下所示: 2021-12-07 13:51:55+00

我正在尝试 select 24 小时内的数据:2021-12-01 00:00:00 和 2021-12-01 23:59:59

我的查询如下所示:

SELECT to_timestamp(loggeddate), to_timestamp(trxdate), [column a], [column b], [column c], [column d]
FROM [this table]
where [column a] like 'some criteria'
or [column a] like 'some other criteria'
and loggeddate between to_timestamp('2021-12-01 00:00:00') and to_timestamp('2021-12-01 23:59:59')

我得到的错误是:

ERROR:  invalid input syntax for type double precision: "2021-12-01 00:00:00"
LINE 5: and loggeddate between to_timestamp('2021-12-01 00:00:00') a...
                                            ^

请有人解释一下显而易见的事情吗?

PostgreSQL 根本不知道如何读取您作为 function 参数传递的字符串。 尝试这个:

SELECT to_timestamp('2021-12-01 23:59:59', 'YYYY-MM-DD HH24:MI:SS') 

您的 WHERE 子句中有两个错误:第一个to_timestamp()不能在没有格式掩码的情况下使用。 这就是我更喜欢 ANSI 时间戳文字的原因之一,例如timestamp '2021-12-01 00:00:00'

在处理时间戳范围条件时,通常最好避免使用 BETWEEN 运算符并使用>=<以及上限之后的日期。

但是,错误消息的根本原因是您将数字与时间戳进行比较。 如果您想要一个可读的 WHERE 条件,您还需要将您的 unix 纪元转换为时间戳:

and to_timestamp(loggeddate) >= timestamp '2021-12-01 00:00:00'
and to_timestamp(loggeddate) <  timestamp '2021-12-02 00:00:00'

暂无
暂无

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

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