Oracle SQL:where 子句中的时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8855378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:00:10  来源:igfitidea点击:

Oracle SQL : timestamps in where clause

sqloraclewheresql-timestamp

提问by sid

I need to look up rows within a particular time frame.

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

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

I.e.: I need to look up rows with timestamp precision of SECONDS. How do I achieve this?

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

FYI: The startdatecolumn is of type TIMESTAMP.

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

回答by a_horse_with_no_name

to_timestamp()

to_timestamp()

You need to use to_timestamp()to convert your string to a proper timestampvalue:

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

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

to_date()

迄今为止()

If your column is of type DATE(which also supports seconds), you need to use to_date()

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

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

Example

例子

To get this into a wherecondition use the following:

要使其成为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')

Note

笔记

You never need to use to_timestamp()on a column that is of type timestamp.

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

回答by jyapx

For everyone coming to this thread with fractional seconds in your timestamp use:

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

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

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