Author |
Topic |
ultradiv
Starting Member
41 Posts |
Posted - 2008-07-11 : 05:31:38
|
Hi FolksTable has field MyDateTime datetimeQuery has input of @InDateTime datetime Need to select * where MyDateTime's (3rd Friday in month) = @InDateTime(which equates to 3rd Friday in month)The InDateTime could of course be Nth occurrence in it's monthI'm sure the answer must be here somewhere but being new to this forum I haven't been able to find it after reading for several hours! Fascinating reading by the way Many thanksAndy |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-07-11 : 05:34:00
|
sorry.... what's the question?Em |
 |
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
ultradiv
Starting Member
41 Posts |
Posted - 2008-07-11 : 05:40:10
|
quote: Originally posted by elancaster sorry.... what's the question?Em
The parameter contains a datetime value and I want to select from the table the datetime values that eqaute to the same occurrence of that parameters day e.g. the second monday in the monthI am finding it difficult to explain - sorry |
 |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-07-11 : 05:46:20
|
so you want to match not on date but on 'position in the month' ? i.e. if the parameter date equates to the '1st monday in a month' you want to return all the '1st monday in a month' ? any month? any year?can you provide some sample data to help with an example of you would like to return / not return?Em |
 |
|
ultradiv
Starting Member
41 Posts |
Posted - 2008-07-11 : 05:52:54
|
quote: Originally posted by elancaster so you want to match not on date but on 'position in the month' ? i.e. if the parameter date equates to the '1st monday in a month' you want to return all the '1st monday in a month' ? any month? any year?can you provide some sample data to help with an example of you would like to return / not return?Em
Yes Exactly The data is very simple so I dont really have a lot to offer you but:select CleanIDfrom dbo.WR_schedulewhere @dateIN = CleanStartthe where clause is what needs attention thanks elancaster |
 |
|
ultradiv
Starting Member
41 Posts |
Posted - 2008-07-11 : 06:09:57
|
One other thing, I am using set @@DATEFIRST = 1in SQL Server 2000 |
 |
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-07-11 : 06:17:23
|
struggling for time today so i've not had chance played with it much yet, but as per 'spirit1's suggestion this is a link to a date table function in the script library...http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519&whichpage=1something like that would let you quickly determine 'where in the month' you are. It's a start anyway Em |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-12 : 01:23:51
|
try this too (not tested)select * where DATEPART(dw,MyDateTime) = DATEPART(dw,@InDateTime)AND DATEPART(wk,MyDateTime)=DATEPART(wk,@InDateTime) |
 |
|
ultradiv
Starting Member
41 Posts |
Posted - 2008-07-12 : 18:31:13
|
quote: Originally posted by visakh16 try this too (not tested)select * where DATEPART(dw,MyDateTime) = DATEPART(dw,@InDateTime)AND DATEPART(wk,MyDateTime)=DATEPART(wk,@InDateTime)
No, the wk is 'of year' and I need to select from any month any year |
 |
|
ultradiv
Starting Member
41 Posts |
Posted - 2008-07-12 : 19:25:26
|
I think the solution may be very simple.WHERE ((DATEPART(d,DateColumn)-1)/7)+1 = ((DATEPART(d,@dateIN)-1)/7)+1and DATEPART(dw,DateColumn) = DATEPART(dw,@dateIN)It seems to work, ((number of day in month)-1)/7)+1or am I missing something here?Q. Does using the DATEPART function on an indexed field nullify the index when selecting?If so Q. Is there a way around this to maintain the fastest access when selecting from this data in this manner? |
 |
|
|