SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

DATEADD and variables in SQL

To make the T-SQL  function DATEADD more dynamic use variables. The example below , accepts an input parameter , in this case “4”.

All events older than 4 months from now will be deleted.


DECLARE @months int
SET @months = 4

DELETE FROM [Events] WHERE [EventDate] < DATEADD (month,-1*@months, getdate())  


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Leave a comment on the original post [www.sqlserver-dba.com, opens in a new window]

Loading comments...