How to get number of working days in a month in SQL

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
chrisjones
Participant
Posts: 194
Joined: Thu May 11, 2006 9:42 am

How to get number of working days in a month in SQL

Post by chrisjones »

Hi Friends,
i need to get the number of workling days in a month using sql query..

so please help me by giving some sample code
Thanks,
Chris Jones
Krazykoolrohit
Charter Member
Charter Member
Posts: 560
Joined: Wed Jul 13, 2005 5:36 am
Location: Ohio

Post by Krazykoolrohit »

Follow this topic being discussed here:

viewtopic.php?t=102642&highlight=number
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

There is no sample code. Whether a day is a business day or not cannot be programmatically determined by anyone. You need a TIME dimension table, from that you can get your answer quite easily.

If you're going to assume that every Monday thru Friday is a working day, you can write something. However, not knowing your database we couldn't even give you an adequate answer. If you have Oracle, I'd suggest a user defined function. But I don't have one to give because I'd never do it this way, your answer is GUARANTEED to be wrong if you need to discount holidays, government holidays, optional holidays, etc.
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
chrisjones
Participant
Posts: 194
Joined: Thu May 11, 2006 9:42 am

How to get number of working days in a month in SQL

Post by chrisjones »

Thanks for your reply,
I am assuming that every Monday thru Friday is a working day and sat and sun as holiday..
i want the SQL code,

kcbland wrote:There is no sample code. Whether a day is a business day or not cannot be programmatically determined by anyone. You need a TIME dimension table, from that you can get your answer quite easily.

If you're going to assume that every Monday thru Friday is a working day, you can write something. However, not knowing your database we couldn't even give you an adequate answer. If you have Oracle, I'd suggest a user defined function. But I don't have one to give because I'd never do it this way, your answer is GUARANTEED to be wrong if you need to discount holidays, government holidays, optional holidays, etc.
Thanks,
Chris Jones
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

DATABASE?
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
hariprasad
Participant
Posts: 1
Joined: Fri Jul 14, 2006 1:03 pm

How to get number of working days in a month in SQL

Post by hariprasad »

My Database is Oracle.


kcbland wrote:DATABASE? ...
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

You have WeekdayFromDate() available in Datastage. Returns the day numberof the week from the given date. You can use this to identify the whether the given date is week day or week end.
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Re: How to get number of working days in a month in SQL

Post by kcbland »

hariprasad wrote:My Database is Oracle.
kcbland wrote:DATABASE? ...
Fantastic, but it's not your question to answer. A person demanding a database specific query solution should have the courtesy to tell us the database they are using.

Else I'll be tempted to answer with SQL for the most arcane RDBMS I can think up. :twisted:
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
meena
Participant
Posts: 430
Joined: Tue Sep 13, 2005 12:17 pm

Re: How to get number of working days in a month in SQL

Post by meena »

Hi Chrisjones....
I did a seacrh in google based on working_days function in oracle. I found a function to calculate number of working days.I am posting the function here.Write a SQL with part of this fucntion and use 'where' to pass the values of the date_one and date_two.

Well hope this may give you some idea...

Code: Select all

CREATE OR REPLACE FUNCTION "WORKING_DAYS_FN" (date_one IN DATE, date_two IN DATE) 
RETURN NUMBER 

IS 
working_days NUMBER; 
BEGIN 
CASE SIGN (date_one - date_two) 
WHEN -1 
THEN 
SELECT COUNT (date_calc) 
INTO working_days 
FROM calendar_table
WHERE date_calc <= date_two 
AND date_calc > date_one 
AND LOWER (TO_CHAR (date_calc, 'day')) NOT LIKE '%sun%' 
AND LOWER (TO_CHAR (date_calc, 'day')) NOT LIKE '%sat%' 
AND holiday IS NULL; 
ELSE 
SELECT COUNT (date_calc) 
INTO working_days 
FROM calendar_table 
WHERE date_calc <= date_one 
AND date_calc > date_two 
AND LOWER (TO_CHAR (date_calc, 'day')) NOT LIKE '%sun%' 
AND LOWER (TO_CHAR (date_calc, 'day')) NOT LIKE '%sat%' 
AND holiday IS NULL; 
END CASE; 

RETURN working_days; 
END;
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

Great meena, you gave him a solution utilizing a table "calendar_table", which is just like a Time dimension....
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
meena
Participant
Posts: 430
Joined: Tue Sep 13, 2005 12:17 pm

Post by meena »

Hi Kenneth Bland..
Thank you..... :D
kcbland wrote:Great meena, you gave him a solution utilizing a table "calendar_table", which is just like a Time dimension.... ...
clshore
Charter Member
Charter Member
Posts: 115
Joined: Tue Oct 21, 2003 11:45 am

Post by clshore »

Tough to do in plain Sql without some kind of calendar table,
but assuming you DO have column 'date' in 'dateTable',
this will count the weekdays in a specified year and month:

select count(*) from dateTable
where to_char(date,'YYYYMM') = '200608' -- specify the year and month
and to_char(date,'D') between 2 and 6 -- get weekdays MO-FR


Carter
prabu
Participant
Posts: 146
Joined: Fri Oct 22, 2004 9:12 am

Re: How to get number of working days in a month in SQL

Post by prabu »

kcbland wrote:
hariprasad wrote:My Database is Oracle.
kcbland wrote:DATABASE? ...
Fantastic, but it's not your question to answer. A person demanding a database specific query solution should have the courtesy to tell us the database they are using.

Else I'll be tempted to answer with SQL for the most arcane RDBMS I can think up. :twisted:

Code: Select all

select 
sum(
case 
 when to_char(trunc(last_day(sysdate))-level,'DY')='MON' then
 1
 when to_char(trunc(last_day(sysdate))-level,'DY')='TUE' then
 1
 when to_char(trunc(last_day(sysdate))-level,'DY')='WED' then
 1
 when to_char(trunc(last_day(sysdate))-level,'DY')='THU' then
 1
 when to_char(trunc(last_day(sysdate))-level,'DY')='FRI' then
 1
else
 0
end
) working_day,
sum(
case 
 when to_char(trunc(last_day(sysdate))-level,'DY')='SUN' then
 1
 when to_char(trunc(last_day(sysdate))-level,'DY')='SAT' then
 1
else
 0
end
) holiday,
count(level) total_days
 from dual
connect by level<to_char(trunc(last_day(sysdate)),'DD')+1
/
i cannot think of a more procedural way, thanks clshore :)
connect by level is a trick , BTW
hope somebody gets benefitted :D
Post Reply