如何在PostgreSQL中按时间查询

huangapple go评论96阅读模式
英文:

How to query by time in postgresql

问题

以下是翻译好的内容:

device_id device_created_at
10e7983e-6a7b-443f-b0fe-d5e6485a502c 2022-08-10 20:55:16.695 Three

我有一个表格,其中我的日期/时间的格式为:2022-08-10 20:55:16.695,这是一个时间戳对象。我尝试了以下查询,但没有返回任何行:

select * from device where to_char(device_created_at,'YYYY-MM-DD HH24:MI:SS.FFF') = '2022-08-10 20:55:16.695'

device_created_at 的类型是 "timestamp without time zone"。

如何在 PostgreSQL 中基于时间戳进行查询?

英文:
device_id device_created_at
10e7983e-6a7b-443f-b0fe-d5e6485a502c 2022-08-10 20:55:16.695 Three

i have a table where my date/time is of form: 2022-08-10 20:55:16.695 This is a timestampped object. I tried the following query but didn't return any rows:

select * from device where to_char(device_created_at,'YYYY-MM-DD HH24:MI:SS.FFF') = '2022-08-10 20:55:16.695'

The type of device_created_at is "timestamp without time zone"

How do i query based on timestamp in postgressql?

答案1

得分: 2

尝试使用时间戳值而不是字符串进行比较:

SELECT * 
FROM device 
WHERE device_created_at = CAST('2022-08-10 20:55:16.695' AS TIMESTAMP)

在此处查看演示:链接

英文:

Try comparing timestamp values in place of strings:

SELECT * 
FROM device 
WHERE device_created_at = CAST('2022-08-10 20:55:16.695' AS TIMESTAMP)

Check the demo here.

huangapple
  • 本文由 发表于 2022年12月12日 06:21:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/74765021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定