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
 Previous Month

Author  Topic 

wsilage
Yak Posting Veteran

82 Posts

Posted - 2013-07-10 : 08:56:41
Hi,

I can get the name of the month by doing this...

SELECT
Datename(month,getdate()) as monthName

But I am not able to get the previous Month? How can I do that I thought by putting -1 it would do that for me, but it doesn't.

What other suggestions can you give me? Thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-10 : 08:59:31
SELECT
Datename(month,DATEADD(mm,DATEDIFF(mm,0,getdate())-1,0)) as monthName

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-07-10 : 09:02:29
If you are on SQL 2012
datename(month,eomonth(getdate(),-1))
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-07-10 : 11:02:38
SELECT
Datename(month,dateadd(month, -1, getdate())) as monthName


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

- Advertisement -