SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Getting Weekdays date between a specified date range


Getting Weekdays date between a specified date range

Author
Message
vibin.das
vibin.das
Old Hand
Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)

Group: General Forum Members
Points: 307 Visits: 116
Hi All,

Following is my requirement.

I have a start date and end date For Ex-
@StartDate = 2010-11-22 and @EndDate = 2010-12-22

I need to get all dates for Monday,similarily for other days within the above date range

ie.For Monday it should return
2010-11-29,2010-12-06,2010-12-13,2010-12-20

Similarily for Tuesday it should return
2010-11-30,2010-12-07,2010-12-14,2010-12-21

Can anybody help me out on this....
Dave Ballantyne
Dave Ballantyne
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17104 Visits: 8370
Try a calendar table

http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html



Clear Sky SQL
My Blog
Sachin Nandanwar
Sachin Nandanwar
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4061 Visits: 2633
OR something like this

declare @StartDate datetime = '2010-11-22'
declare @EndDate datetime = '2010-12-22'
;with DateSequence
as
(

select @StartDate Date
union all
select Date+1 from DateSequence where Date<@EndDate

)

select *,datename(WEEKDAY,Date)Day from DateSequence
order by datepart(WEEKDAY,Date)



--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
ColdCoffee
ColdCoffee
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21211 Visits: 5555
How about this?

DECLARE @StartDate DATETIME --= '2010-11-22'
DECLARE @EndDate DATETIME --= '2010-12-22'

SET @StartDate = '2010-11-22'
SET @EndDate = '2010-12-22'

; WITH Tens (N) AS
(
SELECT 1
UNION ALL SELECT 1
UNION ALL SELECT 1
UNION ALL SELECT 1
UNION ALL SELECT 1
UNION ALL SELECT 1
UNION ALL SELECT 1
UNION ALL SELECT 1
UNION ALL SELECT 1
UNION ALL SELECT 1
),
Thousands (N) AS
(
SELECT T1.N FROM Tens T1 , Tens T2 , Tens T3
),
TallyOnTheFly (N) AS
(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM Thousands
)
SELECT DATEADD(DD ,( N-1 ), @StartDate ) DateCol
FROM TallyOnTheFly
WHERE DATEDIFF(DD ,@StartDate , @EndDate) >= ( N - 1 )



You can extend the TallyOntheFly to any number of your wish ;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search