Oracle SQL:where子句中的时间戳

sid*_*sid 49 sql oracle where sql-timestamp

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

select * 
from TableA 
where startdate >= '12-01-2012 21:24:00' 
  and startdate <= '12-01-2012 21:25:33'
Run Code Online (Sandbox Code Playgroud)

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

a_h*_*ame 105

您需要使用to_timestamp()将字符串转换为正确的时间戳值:

to_timestamp('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')
Run Code Online (Sandbox Code Playgroud)

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

to_date('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')
Run Code Online (Sandbox Code Playgroud)

要使其进入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')
Run Code Online (Sandbox Code Playgroud)

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

编辑更正的拼写错误


jya*_*apx 5

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

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