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."
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())