June 23, 2015 at 7:39 am
I can't take credit for the function. I had that in useful scripts stored on my machine. But its definitely from a blog online.
The rest was me though 🙂
MCITP SQL 2005, MCSA SQL 2012
June 23, 2015 at 7:43 am
Folks. This works like a charm.
Thanx.
June 23, 2015 at 7:47 am
RTaylor2208 (6/23/2015)
I can't take credit for the function. I had that in useful scripts stored on my machine. But its definitely from a blog online.The rest was me though 🙂
I think everyone ends up writing one of those workday scripts at one time or another, myself included. It's how I got my start writing on SQL Server Central...
http://www.sqlservercentral.com/articles/Advanced+Querying/calculatingworkdays/1660/
--Jeff Moden
Change is inevitable... Change for the better is not.
June 24, 2015 at 2:07 pm
Hi. Here's a quick script that returns all dates between two dates provided and whether that date is a weekend or not. you could use this in your scenario as you deem fit. Hope this helps.
--
DECLARE @startDate DATETIME='02/02/2015' -- mm/dd/yyyy
DECLARE @endDate DATETIME= GETDATE() -- mm/dd/yyyy
;WITH Calender (CalanderDate, dayOfTheWeek) AS
(
SELECT @startDate AS CalanderDate, datepart(weekday,@startDate) as dayOfTheWeek
UNION ALL
SELECT CalanderDate + 1, datepart(weekday,CalanderDate + 1) as dayOfTheWeek FROM Calender
WHERE CalanderDate + 1 <= @endDate
)
SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25),
(case when dayOfTheWeek in (1,7) then 'WeekEnd' else 'NotAWeekend' end) [IsWeekEnd]
FROM Calender
OPTION (MAXRECURSION 0)
June 24, 2015 at 3:35 pm
sachin.patke (6/24/2015)
Hi. Here's a quick script that returns all dates between two dates provided and whether that date is a weekend or not. you could use this in your scenario as you deem fit. Hope this helps.--
DECLARE @startDate DATETIME='02/02/2015' -- mm/dd/yyyy
DECLARE @endDate DATETIME= GETDATE() -- mm/dd/yyyy
;WITH Calender (CalanderDate, dayOfTheWeek) AS
(
SELECT @startDate AS CalanderDate, datepart(weekday,@startDate) as dayOfTheWeek
UNION ALL
SELECT CalanderDate + 1, datepart(weekday,CalanderDate + 1) as dayOfTheWeek FROM Calender
WHERE CalanderDate + 1 <= @endDate
)
SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25),
(case when dayOfTheWeek in (1,7) then 'WeekEnd' else 'NotAWeekend' end) [IsWeekEnd]
FROM Calender
OPTION (MAXRECURSION 0)
Ah... be careful now. That's not actually "quick". That uses a recursive CTE that counts and, even for a small number of rows, performance and resource usage is comparatively horrible. Please see the following article for why such things should be avoided even for a small number of rows.
http://www.sqlservercentral.com/articles/T-SQL/74118/
--Jeff Moden
Change is inevitable... Change for the better is not.
June 25, 2015 at 4:21 am
Thanx for the articles.
Viewing 6 posts - 16 through 20 (of 20 total)
You must be logged in to reply to this topic. Login to reply