Calculating holidays: number of saturdays and sundays within the given date range query in Oracle

后端 未结 1 1936
面向向阳花
面向向阳花 2021-01-23 12:56

I want to calculate holidays: the number of Saturdays and Sundays within the given date range query in Oracle.

相关标签:
1条回答
  • 2021-01-23 13:22

    You could use the ROW GENERATOR technique to first generate the dates for a given range, and then count only the SATURDAYs and SUNDAYs.

    For example, this query will give me the total count of saturdays and sundays between 1st Jan 2014 and 31st Dec 2014 -

    SQL> WITH DATA AS
      2    (SELECT to_date('01/01/2014', 'DD/MM/YYYY') date1,
      3      to_date('31/12/2014', 'DD/MM/YYYY') date2
      4    FROM dual
      5    )
      6  SELECT SUM(holiday) holiday_count
      7  FROM
      8    (SELECT
      9      CASE
     10        WHEN TO_CHAR(date1+LEVEL-1, 'DY','NLS_DATE_LANGUAGE=AMERICAN') IN ('SAT', 'SUN')
     11        THEN 1
     12        ELSE 0
     13      END holiday
     14    FROM data
     15      CONNECT BY LEVEL <= date2-date1+1
     16    )
     17  /
    
    HOLIDAY_COUNT
    -------------
              104
    
    SQL>
    
    0 讨论(0)
提交回复
热议问题