Search code examples
sql-serversql-server-2008t-sqlqtsql

I need a query to delete a value in table if value is older than 4 months please look at details


I have a EmailID_tbl that has email address of the users I have got request to "remove emails from EmailID_tbl if last login date for user is within the past 4 months."


Solution

  • Assuming that these are in the same table, you could just use a WHERE clause to determine what needs to be deleted via the DATEADD() function to subtract the appropriate number of months from GETDATE(), which would yield the current date :

    -- This would delete every record in your table with LastLoginDates 
    DELETE 
      FROM EmailID_tbl
     WHERE LastLoginDate < DATEADD(month,-4,GETDATE())