简体   繁体   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'

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.

相关问题 Postgres时间戳不接受2017-01-01T23:00:00-00:00 - Postgres timestamp not accepting 2017-01-01T23:00:00-00:00 postgresql查询以获取12:00到12:00之间的计数 - postgresql query to get counts between 12:00 and 12:00 Postgresql:将日期字符串&#39;2016-01-01 00:00:00&#39;转换为具有特定时区的日期时间 - Postgresql: Convert a date string '2016-01-01 00:00:00' to a datetime with specific timezone 如何在 PostgreSQL 中找到没有日期的 00:00:00 和 23:28:05 之间的时间间隔 - How can I find time interval between 00:00:00 and 23:28:05 without date in PostgreSQL 错误:带时区的时间戳类型的输入语法无效:“09/03/1943 01:00:00 MWT” - ERROR: invalid input syntax for type timestamp with time zone: "09/03/1943 01:00:00 MWT" 如何从当天的 00:00:00 到 11:59:59 之间从 postgresql 获取所有数据? - how can i get the all data from postgresql thats from the current day, between the hours of 00:00:00 and 11:59:59? 如何将 SELECT TO_CHAR 与列表 {11:00:00,12:00:00} - How to SELECT TO_CHAR with list {11:00:00,12:00:00} PgSqlExcetion:日期/时间字段值超出范围:“ 4/20/2012 12:00:00 AM” - PgSqlExcetion: date/time field value out of range: “4/20/2012 12:00:00 AM” Rails:从DateTime字段显示不是00:00或12:00的时间 - Rails: Show time when it's not 00:00 or 12:00 from a DateTime field 使用PGLoader MySQL将Postgres将NULL和&#39;0000-00-00 00:00:00&#39;转换为非NULL - Converting NULL and '0000-00-00 00:00:00' to Non-NULL Using PGLoader MySQL to Postgres
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM