具有Like和'%'的Oracle DB查询时间戳

时间:2018-11-18 13:14:50

标签: sql database oracle timestamp

我想列出TARGET_COMMIT的工作日在10 / Nov / 2018到18 / Nov / 2018之间的所有记录,大约在凌晨5.00点。我一直在使用此查询:

select * from (select * from GGS_ADMIN.GGS_HEARTBEAT_HISTORY  
where DELGROUP='REPDELTA' and TARGET_COMMIT between '10/Nov/2018' 
and '18/Nov/2018') where TARGET_COMMIT like '%/Nov/2018 5:%:%.% AM';

这是我内部查询结果的一条记录:

SBLPROF,    EDELTA, 11/10/2018 5:56:22.064830 AM,   11/10/2018 
5:56:27.495548 AM,
11/10/2018 5:56:24.731541 AM,   2.666711,               11/10/2018 
5:56:26.305759 AM,  REPDELTA    ,   5.430718,   3,  11/10/2018 
5:56:22.820934 AM,  0,  14441,  0,  14441

这里“ 11/11/2018 5:56:27.495548 AM”是我的TARGET_COMMIT字段。

如果使用

select * from (select * from GGS_ADMIN.GGS_HEARTBEAT_HISTORY where 
DELGROUP='REPDELTA' and TARGET_COMMIT between '10/Nov/2018'  and 
'18/Nov/2018') where TARGET_COMMIT like '11/10/2018 5:56:27.495548 AM'

它匹配并列出了结果,而不是我之前的查询。为什么我不能使用“ like”功能和“%”作为时间戳?

这是我的双重结果:

select sysdate from dual; 

  SYSDATE                 
11/18/2018 04:11:53 PM 
1 row selected.

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用TIMESTAMP文字和EXTRACT。对于范围,最好使用>=<而不是BETWEEN

SELECT *
FROM ggs_admin.ggs_heartbeat_history
   WHERE delgroup = 'REPDELTA' AND 
   target_commit    >=  TIMESTAMP '2018-11-10 00:00:00' 
AND target_commit   <   TIMESTAMP '2018-11-18 00:00:00' + INTERVAL '1' DAY
AND EXTRACT ( HOUR FROM target_commit) = 5

如果需要所需的格式,也可以使用

WHERE    
target_commit >=  TO_TIMESTAMP('10/Nov/2018','dd/mon/yyyy')  AND 
target_commit <   TO_TIMESTAMP('18/Nov/2018','dd/mon/yyyy') + INTERVAL '1' DAY
AND EXTRACT ( HOUR FROM target_commit) = 5