Home » SQL & PL/SQL » SQL & PL/SQL » how to get working days each month
how to get working days each month [message #159386] Fri, 17 February 2006 23:31 Go to next message
jchiu183
Messages: 16
Registered: September 2005
Junior Member
How can I change the follow query to get the working days each month without manually enter the value in '&1' and '&2'?


select count(*)
2 from ( select rownum rnum
3 from all_objects
4 where rownum <= to_date('&1') - to_date('&2')+1 )
5 where to_char( to_date('&2')+rnum-1, 'DY' )
not in ( 'SAT', 'SUN' );

Thank you!


Re: how to get working days each month [message #159388 is a reply to message #159386] Sat, 18 February 2006 00:25 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Here's one way:

/forum/fa/825/0/

As for 'ALL_OBJECTS', Look here for more efficient alternatives.
  • Attachment: htmldb2.jpg
    (Size: 42.38KB, Downloaded 4129 times)
Re: how to get working days each month [message #159389 is a reply to message #159388] Sat, 18 February 2006 00:35 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
In a different flavour:
SQL> SELECT themonth
  2       , count(theday) cnt
  3    FROM (  SELECT TO_CHAR(TRUNC(SYSDATE,'YY')+LEVEL-1,'Month') themonth
  4                 , TO_CHAR(TRUNC(SYSDATE,'YY')+LEVEL-1,'DY')    theday
  5              FROM dual
  6           CONNECT BY LEVEL <= ADD_MONTHS(TRUNC(SYSDATE,'YY'),12)-TRUNC(SYSDATE,'YY')
  7         )
  8   WHERE theday NOT IN ('SAT','SUN')
  9   GROUP BY themonth
 10   ORDER BY TO_DATE(themonth,'MM')
 11  /

THEMONTH         CNT
--------- ----------
January           22
February          20
March             23
April             20
May               23
June              22
July              21
August            23
September         21
October           22
November          22
December          21

12 rows selected.


MHE
Re: how to get working days each month [message #159814 is a reply to message #159386] Tue, 21 February 2006 14:22 Go to previous messageGo to next message
jchiu183
Messages: 16
Registered: September 2005
Junior Member
Thank you so much!! It works.
Re: how to get working days each month [message #160017 is a reply to message #159386] Wed, 22 February 2006 15:23 Go to previous messageGo to next message
useebabu
Messages: 49
Registered: February 2005
Location: Boston
Member

Hi Maarten ,

How can we get Sales_Day_No just like you showed count of working days for each month for a given date. For ex: a sales person made a sale on 02/22/2006 that mean Sales_Day_No=16 excluding weekend not holidays. how can we get 16 count. Can you help me with this because I need to populate Sales_Day_No column in the sales table. This column is not there before I have to add column and populate data.

Thanks,

Mohan K.
Re: how to get working days each month [message #160073 is a reply to message #160017] Thu, 23 February 2006 01:24 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
The same principle applies: You use a connect by row generator.
Now we need to count the all days but weekend days. A SUM with DECODE is one option:
SQL> SELECT SUM( -- transform the generated date to abbreviated day of week
  2              DECODE( TO_CHAR(TRUNC(thedate,'MM')+LEVEL-1,'DY')
  3                   , 'SAT', 0 -- Saturdays don't count
  4                   , 'SUN', 0 -- Sundays   don't count
  5                   , 1        -- all other days count
  6                   )
  7            ) sales_day_no
  8       , thedate
  9    FROM ( SELECT TO_DATE('22/02/2006','DD/MM/YYYY') thedate
 10             FROM dual
 11         )
 12   -- row generator technique from 1/2/2006 to 22/2/2006
 13   CONNECT BY TRUNC(thedate,'MM')+LEVEL-1 <= thedate
 14  /

SALES_DAY_NO THEDATE
------------ ---------
          16 22-FEB-06
Some people prefer a case statement. If I were you, I'd put it in a (deterministic) function for readability. Let me demonstrate. Here's a small test script.
CREATE OR REPLACE FUNCTION get_sales_day_no(pid_date IN DATE)
  RETURN NUMBER DETERMINISTIC
IS
  v_return NUMBER;
BEGIN
  SELECT SUM( -- transform the generated date to abbreviated day of week
              DECODE( TO_CHAR(TRUNC(pid_date,'MM')+LEVEL-1,'DY')
                   , 'SAT', 0 -- Saturdays don't count
                   , 'SUN', 0 -- Sundays   don't count
                   , 1        -- all other days count
                   )
            ) sales_day_no
    INTO v_return
    FROM dual
 -- row generator technique
 CONNECT BY TRUNC(pid_date,'MM')+LEVEL-1 <= pid_date;
 
 RETURN v_return;
END get_sales_day_no;
/
sho err

SELECT TO_DATE('22/02/2006','dd/mm/yyyy') yourdate
     , get_sales_day_no(TO_DATE('22/02/2006','dd/mm/yyyy')) sales_day_no
  FROM dual
/

DROP FUNCTION get_sales_day_no
/
It's output is this:
SQL> @C:\useful\orafaq

Function created.

No errors.

YOURDATE  SALES_DAY_NO
--------- ------------
22-FEB-06           16


Function dropped.


MHE
Re: how to get working days each month [message #160186 is a reply to message #159386] Thu, 23 February 2006 14:15 Go to previous message
useebabu
Messages: 49
Registered: February 2005
Location: Boston
Member

Thanks Maarten, it worked perfect I have created deterministic function based on your suggestion and populated Sales_Day_No.

Thanks,

Mohan K.
Previous Topic: How to see the definition of a created Oracle View
Next Topic: Insert with select
Goto Forum:
  


Current Time: Tue Aug 19 08:54:12 CDT 2025