正确的日期输出未到

时间:2018-10-11 11:23:37

标签: sql oracle

我的查询是:

SELECT
  TO_CHAR(TO_DATE(FROM_DATE,'DD-MM-YYYY HH24:MI:SS'),'DD-MM-YY') AS "FROM DATE",
  TO_CHAR(TO_DATE(TO_DATE,'DD-MM-YYYY HH24:MI:SS'),'DD-MM-YY')   AS "TO DATE"
FROM TRN_ACTUAL_TABLE
WHERE TO_CHAR(TO_DATE(FROM_DATE,'DD-MM-YYYY HH24:MI:SS'),'DD-MM-YY') BETWEEN '01-08-2018 00:00' AND '20-08-2018 00:00'
AND IS_DELETE=0;

输出为:

output

在这个10月(10日)相关日期的输出月份中,但是我搜索了8月(8th)日期

2 个答案:

答案 0 :(得分:0)

您应该比较BETWEEN两侧的日期,并且应该在RHS上使用有效的Oracle日期文字。

WHERE
    TO_DATE(FROM_DATE,'DD-MM-YYYY HH24:MI:SS') BETWEEN
        DATE '2018-08-01' AND DATE '2018-08-20' AND ...

答案 1 :(得分:0)

针对2k年的问题,您最好使用RR代替YYRRRR代替YYYY,并摆脱TO_CHAR的转换where子句为:

SELECT TO_CHAR(TO_DATE(FROM_DATE,'DD-MM-RRRR HH24:MI:SS'),'DD-MM-RR') AS "FROM DATE",
       TO_CHAR(TO_DATE(TO_DATE,'DD-MM-RRRR HH24:MI:SS'),'DD-MM-RR')   AS "TO DATE"
  FROM TRN_ACTUAL_TABLE 
 WHERE TO_DATE(FROM_DATE,'DD-MM-RRRR HH24:MI:SS') 
    BETWEEN TO_DATE('01-08-2018 00:00:00','DD-MM-RRRR HH24:MI:SS') 
        AND TO_DATE('20-08-2018 00:00:00','DD-MM-RRRR HH24:MI:SS')
        AND IS_DELETE=0;

Rextester Demo