Oracle查询以获取特定时间之间的数据

时间:2018-10-10 11:15:12

标签: sql oracle

我们正试图找出整个年份在00到03上午之间进行的交易。

尝试对extract和to_date函数进行了很多修改,但无法获得确切的结果。

以下提供了即使未指定日期也仅提供当月数据的信息

and TXNDATE >= to_date('00:00:00', 'HH24:MI:SS')
and TXNDATE <= to_date('03:30:00','HH24:MI:SS')

请协助。

3 个答案:

答案 0 :(得分:0)

只提取小时怎么样?

Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");

如果您想直接处理时间,则字符串可能是最好的方法:

and extract(hour from TXNDATE) < 3

如果性能是一个问题,并且您想使用索引,则可以创建包含to_char(txndate, 'HH24:MI:SS') >= '00:00:00' and to_char(txndate, 'HH24:MI:SS') < '03:00:00' 的索引。

答案 1 :(得分:0)

尝试一下:

WHERE to_char(tnxdate, 'HH24:MI:SS') between '00:00:00' and '03:30:00'

答案 2 :(得分:0)

您可以使用“ EXTRACT”来执行此操作,但必须注意正确编码边缘条件。这是一个示例:

WITH cteTxndates AS (SELECT TO_DATE('01-JAN-2018 00:00:00', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('01-JAN-2018 00:00:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('05-FEB-2018 00:01:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('10-MAR-2018 00:10:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('15-MAR-2018 01:00:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('20-APR-2018 10:00:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- 
                     SELECT TO_DATE('25-MAY-2018 02:00:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('30-JUN-2018 03:00:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('02-JUL-2018 10:00:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- 
                     SELECT TO_DATE('20-AUG-2018 01:00:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('03-SEP-2018 02:30:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('30-OCT-2018 03:30:00', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('30-OCT-2018 03:30:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- 
                     SELECT TO_DATE('04-NOV-2018 02:22:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL UNION ALL  -- *
                     SELECT TO_DATE('14-DEC-2018 01:11:01', 'DD-MON-YYYY HH24:MI:SS') AS TXNDATE FROM DUAL),          -- *
     cteDatefields AS (SELECT TXNDATE,
                              EXTRACT(HOUR FROM CAST(TXNDATE AS TIMESTAMP)) AS XHOUR,
                              EXTRACT(MINUTE FROM CAST(TXNDATE AS TIMESTAMP)) AS XMINUTE,
                              EXTRACT(SECOND FROM CAST(TXNDATE AS TIMESTAMP)) AS XSECOND
                         FROM cteTxndates)
SELECT *
  FROM cteDatefields
  WHERE XHOUR >= 0 AND
        ( XHOUR < 3 OR
         (XHOUR = 3 AND XMINUTE < 30) OR
         (XHOUR = 3 AND XMINUTE = 30 AND XSECOND = 0));

dbfiddle here