Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of Days in a Month excluding Sat and Sun

Status
Not open for further replies.

marydn

Programmer
Mar 26, 2001
152
US
Would anyone know how I would calculate the number of days in a given month excluding Saturdays and Sundays?

I would also like to count the number of Saturdays and Sundays per a given month.

Any help would be greatly appreciated.

M.
 
This should get you started:

Code:
declare @startdate smalldatetime
declare @enddate smalldatetime
declare @datecount smalldatetime

declare @numweekdays int
declare @numweekenddays int

select @numweekdays = 0
select @numweekenddays = 0

select @startdate = '10/01/2003'
select @enddate = '10/31/2003'
select @datecount = @startdate

while @datecount <= @enddate
begin

	if datepart(weekday,@datecount) in (1,7) select @numweekenddays = @numweekenddays + 1 
	else  select @numweekdays = @numweekdays + 1 

	select @datecount = dateadd(day, 1, @datecount)

end

select @numweekdays, @numweekenddays
 
Worked like a charm - Thanks so much!
 
JBY1, so your weekend is Monday(1) and Sunday (7)?

How can @@DATEFIRST affect this code.
 
My weekend is Sunday(1) and Saturday(7). This is the default on my set up.

@@DATEFIRST can affect by the code by altering the First Day of the Week.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top