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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to find the last wednesday of a month...

Author  Topic 

kka_anand
Starting Member

24 Posts

Posted - 2010-10-24 : 10:28:57
Hi All,

I have a requirement to find a last wednesday of a month.
Example,

For the current month October'2010 the last wednesday is 27-Oct-2010.
For the next month November'2010 the last wednesday is 24-Nov-2010.

Thanks in Advance...

Regards
Anand

Sachin.Nand

2937 Posts

Posted - 2010-10-24 : 12:18:14
[code]
declare @dt datetime='12-may-2010'

select case datename(dw,DATEADD(mm, DATEDIFF(mm,-1, @dt),-1))
when 'sunday' then DATEADD(mm, DATEDIFF(mm,-1, @dt),-1)-4
when 'monday' then DATEADD(mm, DATEDIFF(mm,-1, @dt),-1)-5
when 'tuesday' then DATEADD(mm, DATEDIFF(mm,-1, @dt),-1)-6
when 'wednesday' then DATEADD(mm, DATEDIFF(mm,-1, @dt),-1)
when 'thursday' then DATEADD(mm, DATEDIFF(mm,-1, @dt),-1)-1
when 'friday' then DATEADD(mm, DATEDIFF(mm,-1, @dt),-1)-2
when 'saturday' then DATEADD(mm, DATEDIFF(mm,-1, @dt),-1)-3
end

[/code]

PBUH

Go to Top of Page
   

- Advertisement -