63

我需要在特定时间范围内查找行。

select * 
from TableA 
where startdate >= '12-01-2012 21:24:00' 
  and startdate <= '12-01-2012 21:25:33'

即:我需要查找时间戳精度为 SECONDS 的行。我如何实现这一目标?

仅供参考:该startdate列的类型为TIMESTAMP

4

2 回答 2

140

to_timestamp()

您需要使用to_timestamp()将字符串转换为适当的timestamp值:

to_timestamp('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')

迄今为止()

如果您的列是类型DATE(也支持秒),则需要使用to_date()

to_date('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')

例子

要将其纳入where条件,请使用以下命令:

select * 
from TableA 
where startdate >= to_timestamp('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')
  and startdate <= to_timestamp('12-01-2012 21:25:33', 'dd-mm-yyyy hh24:mi:ss')

笔记

您永远不需要to_timestamp()在类型为 的列上使用timestamp

于 2012-01-13T18:37:48.127 回答
13

对于在时间戳中使用小数秒来访问此线程的每个人,请使用:

to_timestamp('2018-11-03 12:35:20.419000', 'YYYY-MM-DD HH24:MI:SS.FF')

于 2019-01-04T20:34:40.797 回答