[英]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'
I have columns that contain unix timestamps - integers representing the number of seconds since the epoch.我有包含 unix 时间戳的列 - 表示自纪元以来的秒数的整数。 They look like this:
1638888715
.它们看起来像这样:
1638888715
。 I am comfortably converting this int into a timestamp using the to_timestamp()
function and arriving at output that looks like this: 2021-12-07 13:51:55+00
我正在使用
to_timestamp()
function 轻松将此 int 转换为时间戳,并到达 output,如下所示: 2021-12-07 13:51:55+00
I am trying to select data between a 24 hour period: 2021-12-01 00:00:00 and 2021-12-01 23:59:59我正在尝试 select 24 小时内的数据:2021-12-01 00:00:00 和 2021-12-01 23:59:59
My query looks like this:我的查询如下所示:
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')
The error I get is:我得到的错误是:
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...
^
Please could somebody explain the blindingly obvious?请有人解释一下显而易见的事情吗?
PostgreSQL simply doesn't know how to read the string you passed as parameter of the function. PostgreSQL 根本不知道如何读取您作为 function 参数传递的字符串。 Try this:
尝试这个:
SELECT to_timestamp('2021-12-01 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
You have two errors in your WHERE clause: first to_timestamp()
can't be used without a format mask.您的 WHERE 子句中有两个错误:第一个
to_timestamp()
不能在没有格式掩码的情况下使用。 That's one of the reasons I prefer ANSI timestamp literals, eg timestamp '2021-12-01 00:00:00'
这就是我更喜欢 ANSI 时间戳文字的原因之一,例如
timestamp '2021-12-01 00:00:00'
When dealing with timestamp range conditions, it's typically better to avoid the BETWEEN operator and use >=
and <
with the day following the upper limiter.在处理时间戳范围条件时,通常最好避免使用 BETWEEN 运算符并使用
>=
和<
以及上限之后的日期。
The root cause of the error message is however that you are comparing a number to a timestamp.但是,错误消息的根本原因是您将数字与时间戳进行比较。 If you want a readable WHERE condition, you also need to convert your unix epoch to a timestamp there:
如果您想要一个可读的 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.