Link to home
Start Free TrialLog in
Avatar of CodeWrangler
CodeWrangler

asked on

Skip weekends when calculating days between 2 given dates - SQL

Hello,

I have 2 dates, @date1 and @date2, can someone show me a way to calculate weekdays only between the two dates without using a calendar table using SQL
Here are some conditions:
1. If the start or end date falls on a weekend, skip those days. eg: 9-18-10 (sat) to 9-25-10 (sun) should return 5
2. If both start and end date are on the same weekend, return 0

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of chapmanjw
chapmanjw
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CodeWrangler
CodeWrangler

ASKER

I will play with that code and see what I can come up with, however, on the comments section of the same page -
This doesn’t seem work if the @startdate is a Sunday.
for ex:
select dbo.fn_WeekdayDiff(’2009-03-22', ’2009-03-22')
— should return 0, returns 1
select dbo.fn_WeekdayDiff(’2009-03-22', ’2009-04-07')
— should return 12, returns 13
nevermind! I didnt read the author's comment below it!
SOLUTION
Avatar of Sharath S
Sharath S
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks all. All solutions worked with some minor differences.