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.
| Author |
Topic |
|
lefrancisco1
Starting Member
2 Posts |
Posted - 2009-08-26 : 05:08:18
|
| Folks, I jus want to find out the second sunday in eery month or given month.Can you please Help me out with this.Ex- August - Second sundy is 9 sEPTEMBER- Second Sunday is 13 lIKEWISE.... cAN YOU PLEASE heLP ME OUT WITH THIS. |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-08-26 : 05:19:49
|
| try thisselect dateadd(dd,8-datepart(dw,'2009-09-01'),'2009-09-01')+7Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
lefrancisco1
Starting Member
2 Posts |
Posted - 2009-08-26 : 06:24:17
|
Hi Senthil Actually its returning correctly but we should find by giving the month if suppose in August what is the second sundayif i need to get the second sunday for july without considering the first day.quote: Originally posted by senthil_nagore try thisselect dateadd(dd,8-datepart(dw,'2009-09-01'),'2009-09-01')+7Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
|
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-26 : 06:34:54
|
| Hi try this ,drop table #CalendarCREATE TABLE #Calendar(Calendar_Date varchar(32),Month_Num smallint)DECLARE @dtDate datetimeSET @dtDate = '1/1/2009'WHILE (@dtDate <= '12/31/2009')BEGININSERT INTO #Calendar(Calendar_Date,Month_Num)SELECT @dtdate,datepart(month,@dtdate)WHERE datename(dw,@dtdate) = 'Sunday'SET @dtDate = DATEADD(dd,1,@dtDate)ENDselect * from ( SELECT row_number() over ( partition by Month_Num order by Month_Num desc) as rid, * FROM #Calendar)twhere t.rid = 2 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-26 : 06:37:42
|
| [code]try thisdeclare @date datetimeselect @date = '8/30/2009'select cast(cast(month(@date)as varchar(2))+'-'+ cast(number as varchar(2))+'-'+ cast(year(@date)as varchar(4))as datetime)as date into #temp from master..spt_values AS vWHERE v.Type = 'P'AND v.number between 1 and 31select date from (select date,datename(dw,date)as name,(select count(*) from #temp where date <= t.date and datename(dw,date) = datename(dw,t.date) group by datename(dw,date) ) cntfrom #temp t) s where cnt = 2 and name = 'sunday'drop table #temp [/code] |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-08-26 : 06:38:54
|
quote: Originally posted by lefrancisco1 Hi Senthil Actually its returning correctly but we should find by giving the month if suppose in August what is the second sundayif i need to get the second sunday for july without considering the first day.quote: Originally posted by senthil_nagore try thisselect dateadd(dd,8-datepart(dw,'2009-09-01'),'2009-09-01')+7Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
If the month is august you must pass the values as "8" consider remaining as default string "2009-08-01"Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
|
|
|
|
|