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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
nevermind! I didnt read the author's comment below it!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks all. All solutions worked with some minor differences.
ASKER
This doesn’t seem work if the @startdate is a Sunday.
for ex:
select dbo.fn_WeekdayDiff(’2009-0
— should return 0, returns 1
select dbo.fn_WeekdayDiff(’2009-0
— should return 12, returns 13