SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 List all dates for a Monday within a date range
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

richardlaw
Yak Posting Veteran

United Kingdom
68 Posts

Posted - 05/04/2013 :  17:36:52  Show Profile  Visit richardlaw's Homepage  Reply with Quote
Hi

I’d like to create a temp table which simply lists all the dates for a Monday within a date range.

Thanks as always

MuMu88
Aged Yak Warrior

547 Posts

Posted - 05/04/2013 :  22:37:09  Show Profile  Reply with Quote
Here is a way to generate the dates of all Mondays with in a date range:


CREATE TABLE #tmp11(MonDates Date);

declare @DateFrom Date
declare @DateTo Date

set @DateFrom ='2013-05-04'
set @DateTo = '2013-06-27'

INSERT INTO #tmp11(MonDates) 
SELECT  DATEADD(dd,  number*7, DATEADD(dd,  (7 - datediff(dd,0, @dateFrom)%7)%7, @dateFrom)) as MonDate
	 from master..spt_values where type = 'p' and number between 0 and datediff(dd, @dateFrom, @dateTo)/7
SELECT * from  #tmp11;

DROP TABLE #tmp11



OR


declare @DateFrom Date
declare @DateTo Date

set @DateFrom ='2013-05-04'
set @DateTo = '2013-06-27'

SELECT AllDates as MonDates from 
(Select DATEADD(d, number, @dateFrom) as AllDates from master..spt_values 
       where type = 'p' and number between 0 and datediff(dd, @dateFrom, @dateTo)) AS D1
WHERE DATENAME(dw, D1.AllDates)In('Monday')
     

Edited by - MuMu88 on 05/04/2013 22:52:22
Go to Top of Page

waterduck
Aged Yak Warrior

Malaysia
964 Posts

Posted - 05/07/2013 :  02:31:51  Show Profile  Reply with Quote

declare @startdate datetime = '2013-05-04'
declare @enddate datetime = '2013-06-27'
;with cte(col)
as
(
select @startdate
union all
select col + 1
from cte
where col <= @enddate
)
select *, DATENAME(dw, col)
from cte
where DATEPART(dw, col) = 2

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 05/07/2013 :  03:59:41  Show Profile  Reply with Quote
I prefer Waterducks methods as it is independent of any system tables. I'm always nervous to use tables like spt_values in production code as its implementation may change in future.
Only change i would have done will be to change the last filter as below


declare @startdate datetime = '2013-05-04'
declare @enddate datetime = '2013-06-27'
;with cte(col)
as
(
select @startdate
union all
select col + 1
from cte
where col <= @enddate
)
select *
from cte
where DATEDIFF(dd,0,col)% 7 = 0


to make it independent of DATEFIRST setting

http://visakhm.blogspot.in/2012/08/creating-server-independent-day.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

waterduck
Aged Yak Warrior

Malaysia
964 Posts

Posted - 05/07/2013 :  04:22:41  Show Profile  Reply with Quote
noted.
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3451 Posts

Posted - 05/07/2013 :  05:30:10  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
or build a calendar table as far into the future you need dates for and index day of week column.

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000