简体   繁体   中英

Date greater than current date range to be decoded to null- SQL Query

I have created the below query to give me a result set within a date range say from 01-Jan-2018 (c_date_from ) and 31-dec-2018 (c_date_to)

Now I have to make a change in this. Say for example for the below data if I pass c_date_from as 01-Jan-2018 and c_date_to as 31-dec-2018. If the end date is 31-Dec-4712 it will treat it as null. It is working fine for all cases. But in the decode part of the code I now want to handle in such a way that if assignment_end_date column is > than the c_date_to it should treat it as null as well. For example :

+-----------+---------------+---------------+-----------------------+---------------------+--------+
| person_id | next_employee | employee_name | assignment_start_date | assignment_end_date | Client |
+-----------+---------------+---------------+-----------------------+---------------------+--------+
|         1 | XYZ           | GEN           | 02-JAN-2018           | 03-jun-18           | DEL    |
|         1 | REC           | GEN           | 04-jun-18             | 01-nov-18           | DEL    |
|         1 | YZ            | GEN           | 02-nov-18             | 01-JAN-2019         | DEL    |
|         2 | FDGD          | PA            | 01-JAN-2018           | 02-APR-2018         | FAR    |
|         2 | FDRY          | PA            | 03-APR-2018           | 31-DEC-4712         | FAR    |
+-----------+---------------+---------------+-----------------------+---------------------+--------+

For the above table, my query fetches the data well and when the assignment_end_date is 31-dec-4712 then it changes it to NULL in the decode. Now i want if the assignment_end_date(person id 1 max date 01-jan-2019) is > c_date_to (31-dec-2018 in this case) then also it treats it as nul l. How can i handle it in the below query in the same de?ode.

 SELECT papf.person_id,
        LEAD(papf.employee_number) OVER( ORDER BY papf.employee_number, paaf.effective_start_date,change_date ) next_employee,
        PAPF.full_name employee_name,    
        to_char(paaf.effective_start_date,'DD-MON-YY') assignment_start_date, 
        DECODE(paaf.effective_end_date, to_date('12/31/4712', 'MM/DD/YYYY'), NULL, paaf.effective_end_date)  assignment_end_date,
        ppd.segment3 Client,       
            from 
                xl.x_current_employees_gtt PAPF , -
                per_all_assignments_f PAAF,
                per_pay_proposals ppp,
                per_position_definitions ppd             
            WHERE 1 = 1
             --AND papf.employee_number in (60180)
                AND ppp.change_date(+) <= c_date_to  
             AND ppp.date_to(+)     >= c_date_from 
             AND ppp.change_date(+) <= paaf.effective_end_date
             AND ppp.date_to(+)     >= paaf.effective_start_date
             AND ppp.assignment_id(+) = paaf.assignment_id
             AND paaf.primary_flag = 'Y'
             AND paaf.assignment_status_type_id = g_assignment_status_type_id
             AND paaf.assignment_type = 'E'
             AND paaf.effective_start_date <= paaf.effective_end_date -- There are some cases, due to which this condition has been included
             AND paaf.effective_start_date <= c_date_to 
             AND paaf.effective_end_date   >= c_date_from 
             AND PAAF.person_id IN ( PAPF.person_id , NVL(papf.sez_person_id,PAPF.person_id) )
            ORDER BY employee_id,paaf.effective_start_date , change_date ; 

It's not clear where c_date_to is coming from, but I assume it's a column in one of these tables. So I would convert the DECODE to a CASE :

CASE WHEN paaf.effective_end_date = DATE '4712-12-31` OR 
     paaf.effective_end_date > c_date_too THEN NULL 
     ELSE paaf.effective_end_date END AS assignment_end_date

I find using a DATE literal a bit shorter and more readable than TO_DATE .

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