简体   繁体   中英

Oracle decode function not working for date

The query does not give correct result. I am giving submit date between 01-Dec-14 to 30-Dec-14 but getting result for Nov.

SELECT DISTINCT O.id,
                pg.name,
                pt.name,
                PP.company_name,
                PP.code,
                O.order_number,
                O.status,
                O.submit_date,
                O.approval_date
FROM   orders O,
       partnerprofile PP,
       partnertype pt,
       partnergroup pg
WHERE  PP.id = O.to_partner_id
       AND Pp.type_id (+) = 8
       AND o.ordering_ratio_flag = Decode('NO', 'YES', '1',
                                                'NO', '0')
       AND pp.group_id = pg.id
       AND Pp.type_id(+) = Pt.id
       AND Decode(NULL, NULL, '1',
                        Pp.code) = Decode(NULL, NULL, '1',
                                                NULL)
       AND submit_date BETWEEN Decode('01-Dec-14', NULL, ( '01-JAN-01' ),
                                                   '01-Dec-14') AND
                                   Decode('30-Dec-14', NULL, ( '01-JAN-01' ),
                                                       '30-Dec-14')
       AND approval_date BETWEEN Decode(p_approved_from, NULL, ( '01-JAN-01' ),
                                                         p_approved_from) AND
                                 Decode(
                                     p_approved_to, NULL, ( '01-JAN-01' ),
                                                    p_approved_to
                                                                              )
       AND Decode(p_status, NULL, '1',
                            O.status) = Decode(p_status, NULL, '1',
                                                         p_status)
ORDER  BY O.id; 

Besides the comment concering allways to use to_date /datefunctions if you indeed have date-data-types your using of decode doesn't make sense:

Decode('01-Dec-14', NULL, ( '01-JAN-01' ), '01-Dec-14') 
--this is always (!) '01-Dec-14'

Decode "if then else " works like this:

SELECT product_id,
       DECODE (warehouse_id, 1, 'Southlake', 
                             2, 'San Francisco', 
                             3, 'New Jersey', 
                             4, 'Seattle',
                                'Non domestic') 
       "Location of inventory" FROM inventories
       WHERE product_id < 1775;

This example decodes the value warehouse_id. If warehouse_id is 1, then the function returns 'Southlake'; if warehouse_id is 2, then it returns 'San Francisco'; and so forth. If warehouse_id is not 1, 2, 3, or 4, then the function returns 'Non domestic'.

( http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions040.htm )

In your case you always check against the fixed value '01-Dec-14' , as this is not null the else case '01-Dec-14' is returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM