Oracle timestamp类型是否可以直接和日期类型比较大小

Oracle timestamp类型是否可以直接和日期类型比较大小

前言

一般时间戳的字段比较范围用time >= to_timestamp来。

今天发现一条SQL,发现时间戳类型的字段使用了CAST作类型转换为DATE类型,然后在去和DATE类型做比较。

这样做导致了无法使用该字段上的索引,后来建议直接去掉函数处理部分。

改为:

以前处理故障用到gv$active_session_history.sample_time或者dba_hist_active_sess_history.sample_time倒是直接是类似如下使用,

select .... from gv$active_session_history where sample_time >=to_date('2021-01-05 08:00:00','yyyy-mm-dd hh24:mi:ss')

and sample_time <=to_date('2021-01-05 08:05:00','yyyy-mm-dd hh24:mi:ss');

也是可以使用的。这里还是做个试验看看。

环境构造

22:16:54 ZKM@test(1028)> create table aa as select sample_time from gv$active_session_history;

Table created.

Elapsed: 00:00:00.29
22:17:00 ZKM@test(1028)> desc aa;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 SAMPLE_TIME                                        TIMESTAMP(3)

22:17:11 ZKM@test(1028)> alter session set nls_timestamp_format='yyyy-mm-dd hh24:mi:ss.ff9';

Session altered.

Elapsed: 00:00:00.01
22:17:15 ZKM@test(1028)> col MIN(SAMPLE_TIME) for a30
22:17:19 ZKM@test(1028)> col MAX(SAMPLE_TIME) for a30
22:17:19 ZKM@test(1028)> set line 500
22:17:19 ZKM@test(1028)> select min(sample_time),max(sample_time) from aa;

MIN(SAMPLE_TIME)               MAX(SAMPLE_TIME)
------------------------------ ------------------------------
2021-01-05 08:29:45.993000000  2021-01-05 22:16:58.509000000

Elapsed: 00:00:00.52
22:26:24 ZKM@test(1028)> alter session set nls_timestamp_format='yyyy-mm-dd hh24:mi:ss.ff';

Session altered.

Elapsed: 00:00:00.00
22:26:43 ZKM@test(1028)> select min(sample_time),max(sample_time) from aa; 

MIN(SAMPLE_TIME)               MAX(SAMPLE_TIME)
------------------------------ ------------------------------
2021-01-05 08:29:45.993        2021-01-05 22:16:58.509

Elapsed: 00:00:00.01

试验过程

比如查找aa表中时间小于等于"2021-01-05 08:29:45"的条数,按上边数据来看应该是一条都没有。

我们先试用to_timestamp看看,确实一条都查不出来(因为"2021-01-05 08:29:45"其实就是"2021-01-05 08:29:45.000"):

22:27:55 ZKM@test(1028)> select * from aa where sample_time <= to_timestamp('2021-01-05 08:29:45','yyyy-mm-dd hh24:mi:ss');

no rows selected

Elapsed: 00:00:00.01

显然表中最小的2021-01-05 08:29:45.993比条件2021-01-05 08:29:45.000还要大,因此没有符合条件的数据。

但是如果查找aa表中时间小于等于"2021-01-05 08:29:45.993"的条数,那就会有一条符合:

22:27:59 ZKM@test(1028)> select * from aa where sample_time <= to_timestamp('2021-01-05 08:29:45.993','yyyy-mm-dd hh24:mi:ss.ff');

SAMPLE_TIME
---------------------------------------------------------------------------
2021-01-05 08:29:45.993

Elapsed: 00:00:00.01

如果使用to_date的话,查找aa表中时间小于等于"2021-01-05 08:29:45"的条数,一样的道理,不会有数据:

22:28:06 ZKM@test(1028)> select * from aa where sample_time <= to_date('2021-01-05 08:29:45','yyyy-mm-dd hh24:mi:ss');

no rows selected

Elapsed: 00:00:00.01

由于date类型精确度的问题,无法使用类似"2021-01-05 08:29:45.993"的写法,若是要出现这条数据,只能45s出改为46s。

22:28:43 ZKM@test(1028)> select * from aa where sample_time <= to_date('2021-01-05 08:29:46','yyyy-mm-dd hh24:mi:ss');

SAMPLE_TIME
---------------------------------------------------------------------------
2021-01-05 08:29:45.993

Elapsed: 00:00:00.00

结论

时间戳类型直接和时间类型做比较是可以的,稍微注意下精度的问题即可。

原文地址:https://www.cnblogs.com/PiscesCanon/p/14238568.html