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 2008 Forums
 Transact-SQL (2008)
 delete last two mpnths

Author  Topic 

inbs
Aged Yak Warrior

860 Posts

Posted - 2011-10-23 : 10:49:20
i have aggregate table:

how do i delete last two month every day ?
(i know insert last two month i have that date in the row)
month year 
01 2009
02 2009
03 2009
...
11 2009
12 2009

01 2010
02 2010
03 2010
...

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-23 : 10:56:27
[code]
DELETE
FROM table
WHERE month IN (DATEPART(mm,DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0)),DATEPART(mm,DATEADD(mm,DATEDIFF(mm,0,GETDATE())-2,0)))
AND year IN (DATEPART(yy,DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0)),DATEPART(yy,DATEADD(mm,DATEDIFF(mm,0,GETDATE())-2,0)))
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -