首页 > 解决方案 > 如何在具有长格式时间戳的列上以日期作为输入运行 postgres 查询

问题描述

我想查询 postgres 数据库表,该表的时间戳为长毫秒。但我有这样的日期格式“yyyy-MM-dd HH:mm:ssZ”的时间。如何将此日期格式转换为长毫秒来运行查询?

标签: postgresqltimestampsql-timestamp

解决方案


您可以将 long 值转换为适当的时间戳:

select *
from the_table
where to_timestamp(the_millisecond_column / 1000) = timestamp '2020-10-05 07:42'

或者从时间戳值中提取秒数:

select *
from the_table
where the_millisecond_column = extract(epoch from timestamp '2020-10-05 07:42') * 1000

然而,更好的解决方案是将该列转换为正确的timestamp列,以避免(毫秒)和正确timestamp值之间的不断转换


推荐阅读