在START和END日期之间选择DATES的问题

时间:2018-10-26 18:01:22

标签: oracle

表中TRANSACTION_DATE的值如下

TRANSACTION_DATE in tables
    13-APR-17
    06-JUL-17
    17-JUL-17

从以下值的FROM UI传递到开始和结束日期:

V_TRANSACTION_DATE_START := 06-JUL-17
V_TRANSACTION_DATE_END : = 06-JUL-18

以下条件检查日期,我想获得7月2日的交易,但我的查询没有选择任何人

NVL(TRANSACTION_DATE,TRUNC(SYSDATE)) BETWEEN NVL(V_TRANSACTION_DATE_START,NVL(TRANSACTION_DATE,TRUNC(SYSDATE))) 
                                                            AND NVL(V_TRANSACTION_DATE_END,NVL(TRANSACTION_DATE,TRUNC(SYSDATE)))

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

考虑以下代码块:

declare
  V_TRANSACTION_DATE_START varchar2(80) := '06-JUL-17';
  V_TRANSACTION_DATE_END   varchar2(80) := '06-JUL-18'; 

  TRANSACTION_DATE         varchar2(80) := '17-JUL-17'; 
begin

  if NVL(TRANSACTION_DATE,TRUNC(SYSDATE)) 
     BETWEEN NVL(V_TRANSACTION_DATE_START,NVL(TRANSACTION_DATE,TRUNC(SYSDATE))) 
         AND NVL(V_TRANSACTION_DATE_END,NVL(TRANSACTION_DATE,TRUNC(SYSDATE))) then
    dbms_output.put_line('Char collation returns '||
                                        TO_CHAR(TO_DATE(TRANSACTION_DATE,'DD-MON-YY'),'DD-MM-YYYY'));
  end if;     

  if NVL(TO_DATE(TRANSACTION_DATE,'DD-MON-YY'),TRUNC(SYSDATE)) 
     BETWEEN NVL(TO_DATE(V_TRANSACTION_DATE_START,'DD-MON-YY'),NVL(TRANSACTION_DATE,TRUNC(SYSDATE))) 
         AND NVL(TO_DATE(V_TRANSACTION_DATE_END  ,'DD-MON-YY'),NVL(TRANSACTION_DATE,TRUNC(SYSDATE))) then
    dbms_output.put_line('Date collation returns '||
                                        TO_CHAR(TO_DATE(TRANSACTION_DATE,'DD-MON-YY'),'DD-MM-YYYY'));                                          
  end if;                                          
end;   

仅返回第二条语句的结果:

Date collation returns 17-07-2017

结果。

因此,您需要使用正确的格式进行日期转换(此处是DD-MON-YY

Rextester Demo