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

Author  Topic 

direrayne
Starting Member

30 Posts

Posted - 2010-09-14 : 17:29:50
I have tried Google but i cannot find a command for " previous Month "
i know it exist. i need a command the will locate the previous month no matter what when it is ran.

example: i want to schedule a job to run every month on the 2nd to pull data from the month before.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-14 : 17:38:24

declare @d datetime

set @d = getdate()

print @d

set @d = dateadd(dd, -day(@d)+1, dateadd(mm, -1, @d))

print @d

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2010-09-14 : 17:39:23
read about DATEADD() function

http://msdn.microsoft.com/en-us/library/ms186819.aspx
Go to Top of Page

direrayne
Starting Member

30 Posts

Posted - 2010-09-14 : 17:44:18
Thanks guys :)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-14 : 18:09:11
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -