Oracle SQL:where子句中的时间戳 [英] Oracle SQL : timestamps in where clause

查看:163
本文介绍了Oracle SQL:where子句中的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

即:我需要以SECONDS的时间戳精度查找行.我该如何实现?

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

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

FYI: The startdate column is of type TIMESTAMP.

推荐答案

to_timestamp()

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

to_timestamp()

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

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

to_date()

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

to_date()

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

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

示例

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

Example

To get this into a where condition use the following:

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().

Note

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

这篇关于Oracle SQL:where子句中的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆