带日期范围的 where 子句给出奇怪的结果

where clause with date range give strange result

在 xml 发布者报告中,我有以下 where 子句:

    where  NVL(:P_ORDER_NUMBER, xx.order_number) = xx.order_number  
     and xx.ordered_date between NVL(TRUNC(fnd_date.canonical_to_date(:P_FROM_DATE)), xx.ORDERED_DATE) and NVL(TRUNC(fnd_date.canonical_to_date(:P_TO_DATE)), xx.ORDERED_DATE)  
    AND NVL(:P_CUSTOMER, xx.customer_id) = xx.customer_id

但它的行为很奇怪,在日期范围内它的结果是好的,但在日期范围外它的结果是 NULL。

我也尝试了以下方法:

where  NVL(:P_ORDER_NUMBER, xx.order_number) = xx.order_number  
 and ( NVL(TRUNC(fnd_date.canonical_to_date(:P_FROM_DATE)), xx.ORDERED_DATE) > xx.ordered_date  
       or  NVL(TRUNC(fnd_date.canonical_to_date(:P_TO_DATE)), xx.ORDERED_DATE) < xx.ordered_date )  
AND NVL(:P_CUSTOMER, xx.customer_id) = xx.customer_id 

在并发程序中,日期参数有 FND_STANDARD_DATE。

给这只猫剥皮的方法有很多种,这只是其中一种方法。顺便说一下,在您的示例中,您给出的日期是 4 月 31 日,这不存在。

SELECT TO_Char(ordered_date,'DD-MON-YYYY') as ordered_date,
    order_number, customer_name   
FROM order_tbl
WHERE NVL(:P_ORDER_NUMBER, order_number) = order_number
  AND ordered_date between NVL(TO_DATE(:P_FROM_DATE,'DD-MON-YYYY'),TO_DATE('01-MAR-1900','DD-MON-YYYY')) and NVL(to_date(:P_TO_DATE,'DD-MON-YYYY'),TO_DATE('31-DEC-2100','DD-MON-YYYY'))   
  AND NVL(:P_CUSTOMER_NAME, customer_name) = customer_name