Oracle SQL:where子句中的时间戳

问题描述 投票:42回答:2

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

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

即查找时间戳精度为秒的行,我该如何实现?

sql oracle where sql-timestamp
2个回答
92
投票

您需要使用to_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')

你永远不需要在`timestamp'类型的列上使用to_timestamp()

编辑更正的拼写错误


2
投票

对于每个使用时间戳使用时间小数秒来到此主题的人:

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

© www.soinside.com 2019 - 2024. All rights reserved.