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
 Last Monday of Last Month

Author  Topic 

dutchgold647
Starting Member

13 Posts

Posted - 2013-05-28 : 23:13:17
Hi there,

Is it possible to find the last Monday of the previous month?

I can get the Monday of the current week, find last day of previous week/month/year but can't figure out a way to get the last Monday of the previous month.

Any ideas?

Thanks

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-05-28 : 23:52:32
[CODE]

DECLARE @mydate DATE = '2013-05-28'


SELECT DATEADD(dd, - (DATEDIFF(dd, 0, EOlastmonth)%7), EOlastmonth) FROM
(SELECT DATEADD(mm, -1, EOMONTH(@mydate)) as EOlastmonth) A;

[/CODE]
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-29 : 00:16:44
[code]-- MSSQL 2008 solution
DECLARE @mydate DATE = '2013-05-28'
SELECT DATEADD(dd, - (DATEDIFF(dd, 0, EOlastmonth)%7), EOlastmonth) FROM
(SELECT DATEADD(MONTH, DATEDIFF(MONTH, -1, @mydate)-1, -1) as EOlastmonth) A;[/code]

Note: @MuMu88's, EOMONTH() is available in MSSQL 2012

--
Chandu
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2013-05-29 : 00:24:27
SELECT DATEADD(d, -CAST(CAST(EOMONTH(GETDATE(), -1) AS datetime) AS INT)%7, EOMONTH(GETDATE(), -1))
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2013-05-29 : 00:27:01
woops, too late to reply
Go to Top of Page

dutchgold647
Starting Member

13 Posts

Posted - 2013-05-29 : 00:29:30
Thank you all for getting back to me.

My problem has been solved!

Thanks
Go to Top of Page
   

- Advertisement -