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
 help with a statemetn

Author  Topic 

gavakie
Posting Yak Master

221 Posts

Posted - 2008-12-18 : 16:43:23
I need to write a where clause things are equeal to the previous month of the current year but write it so if its january it takes December and of course the previous year.

singularity
Posting Yak Master

153 Posts

Posted - 2008-12-18 : 20:13:33
dateadd(m, -1, getdate()) will subtract one month from the current date. Not sure what format you need this in, but you can use the month() and year() functions to extract the appropriate date parts.
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2008-12-18 : 23:01:10
SELECT * FROM yourtable WHERE MONTH(createdon)=MONTH(GETDATE())-1 AND YEAR(createdon) = CASE WHEN MONTH(GETDATE()) = 1 THEN YEAR(GETDATE()) -1 ELSE YEAR(GETDATE()) END

Jai Krishna
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2008-12-18 : 23:04:25
select * from Table where createdon between DATEADD(m,-1,Getdate()) and Getdate()


I Struggle For Excellence
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2008-12-18 : 23:14:22
Try This Also,,
select * from YourTable where createdon BETWEEN DATEADD(D,1,DATEADD(d,-DAY(DATEADD(m,1,GETDATE())),DATEADD(m,-1,GETDATE())) ) AND DATEADD(D,-1,DATEADD(D,-DAY(GETDATE()-1),GETDATE()))

I Struggle For Excellence
Go to Top of Page
   

- Advertisement -