How do i pull data between current date and current date + 2 but exclude weekends?

51 Views Asked by At

I have a report that runs daily, it shows orders due but between sysdate and sysdate +2. The only issue with this is as we don't work weekends, there's never any records for SAT/SUN.

When the report runs on Friday to pick up orders due today and Monday, Monday is ignored because the sysdate + 2 includes weekend. So it's only returning orders for today.

I've got this far, but it still doesn't totally overlook SAT/SUN, but would only exclude it if it did exist.

Any ideas?

SELECT order_header.ship_by_date 
FROM order_header 
WHERE (order_header.ship_by_date >= TRUNC(sysdate) AND order_header.ship_by_date < TRUNC(SYSDATE) + 2 
    AND 
       (order_header.ship_by_date IN (SELECT order_header.ship_by_date 
        FROM ORDER_HEADER 
        WHERE TO_CHAR(order_header.ship_by_date, 'DAY') NOT IN ('SAT','SUN'))))

Below is the query i ran:

 FROM order_header
WHERE     order_header.ship_by_date >= TRUNC (SYSDATE)
       AND order_header.ship_by_date <
                TRUNC (SYSDATE)
              + CASE WHEN TO_CHAR (SYSDATE, 'DY') = 'FRIDAY' THEN 4 ELSE 2 END```

Results returned:

   SHIP_BY_DATE
31-JAN-20 00.00.00.000000000
31-JAN-20 00.00.00.000000000
31-JAN-20 00.00.00.000000000
31-JAN-20 00.00.00.000000000
31-JAN-20 00.00.00.000000000
31-JAN-20 00.00.00.000000000

If i run this query i do get Monday's results, but it doesn't help as it includes SAT/SUN. You can see how it returned 31-jan and 03-feb.

  FROM order_header
 WHERE order_header.ship_by_date >= TRUNC (SYSDATE)
        AND order_header.ship_by_date <
                 TRUNC (SYSDATE) +4

SHIP_BY_DATE
31-JAN-20 10.14.00.000000000
31-JAN-20 10.17.00.000000000
31-JAN-20 11.09.00.000000000
31-JAN-20 11.20.00.000000000
03-FEB-20 00.00.00.000000000
03-FEB-20 00.00.00.000000000
1

There are 1 best solutions below

6
Littlefoot On

If today is Friday, add 4 days instead of 2:

SELECT order_header.ship_by_date
  FROM order_header
 WHERE (    order_header.ship_by_date >= TRUNC (SYSDATE)
        AND order_header.ship_by_date <
                 TRUNC (SYSDATE)
               + CASE WHEN TO_CHAR (SYSDATE, 'DY') = 'FRI' THEN 4 ELSE 2 END     --> this
        AND (order_header.ship_by_date IN (SELECT order_header.ship_by_date
                                             FROM ORDER_HEADER
                                            WHERE TO_CHAR (
                                                     order_header.ship_by_date,
                                                     'DY') NOT IN ('SAT', 'SUN'))))