Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to find first saturday of a month

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]

Go to Top of Page

mfemenel
Professor Frink

1421 Posts

Posted - 2009-06-17 : 10:58:58
visakh that gave me a date of 6/20

Turn this into a function with a month/year parameter.

declare @month tinyint
declare @year int
declare @datestring varchar(20)
declare @isSat bit
set @isSat=0
set @year=2009
set @month=6

declare @counter tinyint
set @counter=1
set @datestring=CAST(@month as varchar(2)) + '/' + CAST(@counter as varchar(2)) + '/' + CAST(@year as varchar(4))
select @datestring

while @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"
Go to Top of Page

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 tinyint
declare @year int
declare @datestring varchar(20)
declare @isSat bit
set @isSat=0
set @year=2009
set @month=6

declare @counter tinyint
set @counter=1
set @datestring=CAST(@month as varchar(2)) + '/' + CAST(@counter as varchar(2)) + '/' + CAST(@year as varchar(4))
--select @datestring

WHILE @isSat=0
BEGIN
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))
END
END
select @datestring
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-06-17 : 12:13:38
I can not get day of 3rd and 4th week
Go to Top of Page

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'
) a
order by
a.DATE

Results:
DATE FirstSatofMonth
----------------------- -----------------------
2008-02-01 00:00:00.000 2008-02-02 00:00:00.000
2008-02-07 00:00:00.000 2008-02-02 00:00:00.000
2008-02-29 00:00:00.000 2008-02-02 00:00:00.000
2009-06-01 00:00:00.000 2009-06-06 00:00:00.000
2009-06-19 00:00:00.000 2009-06-06 00:00:00.000
2009-06-30 00:00:00.000 2009-06-06 00:00:00.000

(6 row(s) affected)


[/code]

CODO ERGO SUM
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-17 : 15:37:25
Also see
http://weblogs.sqlteam.com/peterl/archive/2009/06/17/How-to-get-the-Nth-weekday-of-a-month.aspx


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

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.
Go to Top of Page

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
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-06-18 : 10:03:04
thanks, i got it
Go to Top of Page
   

- Advertisement -