May 11, 2005 at 5:45 am
Could you help me on how to get the no of weekenddays within 2 given dates.
Thanks in anticipation
May 11, 2005 at 6:54 am
See if this helps: http://www.aspfaq.com/show.asp?id=2519
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
May 11, 2005 at 7:39 am
SET DATEFIRST 7
SELECT ((DATEDIFF(week,[startdate],[enddate]) -
(CASE WHEN DATEPART(weekday,[startdate]) IN (1,7)
OR DATEPART(weekday,[enddate]) IN (1,7) THEN 1 ELSE 0 END)) * 2) +
(CASE WHEN DATEPART(weekday,[startdate]) = 7 THEN 1 ELSE 0 END) +
(CASE WHEN DATEPART(weekday,[enddate]) = 1 THEN 1 ELSE 0 END)
Far away is close at hand in the images of elsewhere.
Anon.
May 11, 2005 at 7:45 am
As I've said, use a calendar table.
Hi Dave,
long time no see!
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
May 11, 2005 at 8:03 am
As I've said, use a calendar table. |
Maybe, maybe not, a lot of trouble for small return
All depends on how and how often it is required
long time no see! |
Yeah, seems like a long time.
I notice that your posts seem to be a lot of references
Fed up of writing your own answers
Far away is close at hand in the images of elsewhere.
Anon.
May 11, 2005 at 8:53 am
Nah, that way I can increase postcount even more quickly
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
May 11, 2005 at 1:34 pm
Here's a way that doesn't use a calendar table. It counts the # of weekdays though...
DECLARE @dateStart DATETIME
DECLARE @dateEnd DATETIME
SET @dateStart = '2/1/2004'
SET @dateEnd = '2/29/2004'
--adjust start weekend date to Monday
IF DATEPART(dw,@dateStart) = 7
SET @dateStart = @dateStart + 2
IF DATEPART(dw,@dateStart) = 1
SET @dateStart = @dateStart + 1
--adjust end weekend day to Friday
IF DATEPART(dw,@dateEnd) = 7
SET @dateEnd = @dateEnd - 1
IF DATEPART(dw,@dateEnd) = 1
SET @dateEnd = @dateEnd - 2
SELECT DATEDIFF(dd, @dateStart, @dateEnd) / 7 * 5
+
CASE
WHEN
DATEDIFF(dd, @dateStart, @dateEnd) % 7 = 6
THEN 5
WHEN
DATEPART(dw, @dateStart) > DATEPART(dw, @dateEnd)
THEN DATEDIFF(dd, @dateStart, @dateEnd) % 7 - 1
ELSE DATEDIFF(dd, @dateStart, @dateEnd) % 7 + 1
END
May 11, 2005 at 10:36 pm
This'll work if you have a DBA that doesn't understand the merits of a date table and it's short enough to use in inline code if the DBA won't let you build a UDF, either... it relies on the fact that "wk" is not a 7 day counter... "wk" only increments in value when the day of the week changes from Saturday to Sunday.... that only happens on whole weekends which is why the *2...
SELECT DATEDIFF(wk,@StartDate,@EndDate)*2
+ CASE WHEN DATENAME(dw,@StartDate) = 'Sunday' THEN 1 ELSE 0 END
+ CASE WHEN DATENAME(dw,@EndDate) = 'Saturday' THEN 1 ELSE 0 END
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply