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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Date Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Leigh79
Starting Member

28 Posts

Posted - 12/12/2010 :  06:45:58  Show Profile  Reply with Quote
Hi guys

I'm wondering if this is possible.

In my events table I have the following columns: Start Date, End Date and 7 columns for the days of the week which are set as INT with the value of 0 or 1 if true.

If there is a single entry in the table with a start date of the 18th Nov 2010 and end date of 30th Dec 2010 and the Thursday field is flagged as 1 would it be possible to return a resultset which would display each Thursday between and including the two dates?

Thanks in advance!
Leigh

SwePeso
Patron Saint of Lost Yaks

Sweden
29910 Posts

Posted - 12/12/2010 :  07:00:41  Show Profile  Visit SwePeso's Homepage  Reply with Quote

SELECT		DATEADD(DAY, v.number, x.StartDate) AS Thursday
FROM		(
			SELECT		StartDate,
					EndDate
			FROM		dbo.Table1
			GROUP BY	StartDate,
					EndDate
			HAVING		SUM(CASE WHEN Thursday = 1 THEN 1 ELSE 0 END) = 1
		) AS x
INNER JOIN	master..spt_values AS v ON v.type = 'P'
			AND v.number BETWEEN 0 AND DATEDIFF(DAY, x.StartDate, x.EndDate)
WHERE		DATEDIFF(DAY, 0, DATEADD(DAY, v.number, x.StartDate)) % 7 = 3



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

dataguru1971
Flowing Fount of Yak Knowledge

USA
1464 Posts

Posted - 12/12/2010 :  07:06:46  Show Profile  Send dataguru1971 an AOL message  Send dataguru1971 a Yahoo! Message  Reply with Quote
SELECT StartDate,Enddate
FROM [Table]
Where (datepart(WEEKDAY,startdate) = 5 or (datepart(WEEKDAY,enddate)) = 5
And StartDate >= '2010118' and EndDate <='20101230'

The above will return each startdate and enddate where the date is a thursday and between the dates you indicated


You don't really need the 7 columns. The date functions allow you to identify the weekday pretty simply.





Poor planning on your part does not constitute an emergency on my part.

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.55 seconds. Powered By: Snitz Forums 2000