簡體   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