| Author |
Topic |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-06-17 : 10:40:30
|
| how do we find first saturday of a month in sql server 2005? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-17 : 10:57:05
|
| [code]SELECT dateadd(mm,(datediff(mm,'17530101',GETDATE())/7)*7,'17530101')+5[/code] |
 |
|
|
mfemenel
Professor Frink
1421 Posts |
Posted - 2009-06-17 : 10:58:58
|
| visakh that gave me a date of 6/20Turn this into a function with a month/year parameter.declare @month tinyintdeclare @year intdeclare @datestring varchar(20)declare @isSat bitset @isSat=0set @year=2009set @month=6declare @counter tinyintset @counter=1set @datestring=CAST(@month as varchar(2)) + '/' + CAST(@counter as varchar(2)) + '/' + CAST(@year as varchar(4))select @datestringwhile @isSat=0 begin select @isSat=case DATENAME(weekday,@datestring) when 'Saturday' then 1 else 0 END set @counter=@counter+1 if @isSat=0 begin set @datestring=CAST(@month as varchar(2)) + '/' + CAST(@counter as varchar(2)) + '/' + CAST(@year as varchar(4)) END END select @datestring Mike"oh, that monkey is going to pay" |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-06-17 : 11:23:31
|
| I tried to get 3rd or 4th Saturday of month, but can not get.It works just 1st and 2nd Saturday of month.declare @month tinyintdeclare @year intdeclare @datestring varchar(20)declare @isSat bitset @isSat=0set @year=2009set @month=6declare @counter tinyintset @counter=1set @datestring=CAST(@month as varchar(2)) + '/' + CAST(@counter as varchar(2)) + '/' + CAST(@year as varchar(4))--select @datestringWHILE @isSat=0BEGIN select @isSat=case DATENAME(weekday,@datestring) when 'Saturday' then 1 else 0 END set @counter=@counter+3 if @isSat=0 BEGIN set @datestring=CAST(@month as varchar(2)) + '/' + CAST(@counter as varchar(2)) + '/' + CAST(@year as varchar(4)) ENDENDselect @datestring |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-06-17 : 12:13:38
|
| I can not get day of 3rd and 4th week |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2009-06-17 : 12:38:52
|
| [code]select a.DATE, FirstSatofMonth = dateadd(dd,(datediff(dd,-53685,dateadd(mm,datediff(mm,0,a.DATE),6))/7)*7,-53685)from ( -- Test Data select DATE = convert(datetime,'20090601') union all select DATE = '20090619' union all select DATE = '20090630' union all select DATE = convert(datetime,'20080201') union all select DATE = '20080207' union all select DATE = '20080229' ) aorder by a.DATEResults:DATE FirstSatofMonth----------------------- -----------------------2008-02-01 00:00:00.000 2008-02-02 00:00:00.0002008-02-07 00:00:00.000 2008-02-02 00:00:00.0002008-02-29 00:00:00.000 2008-02-02 00:00:00.0002009-06-01 00:00:00.000 2009-06-06 00:00:00.0002009-06-19 00:00:00.000 2009-06-06 00:00:00.0002009-06-30 00:00:00.000 2009-06-06 00:00:00.000(6 row(s) affected)[/code]CODO ERGO SUM |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-06-18 : 09:44:33
|
| Still, i don't get day of 3rd and 4th Saturday in a month. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2009-06-18 : 09:56:57
|
| You said you wanted to find the first Saturday of the month.If you also want the second, third, or fourth Saturday, have you tried adding 7, 14, or 21 days to the first Saturday?CODO ERGO SUM |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-06-18 : 10:03:04
|
| thanks, i got it |
 |
|
|
|