-4

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

  • 1
    What details are there to look at? – flyingmeatball May 09 '16 at 18:07
  • Should that request actually say "remove email from EmailID_tbl if last login date for user is older than 4 months"? As it is, that would indicate that you would keep users who haven't logged on, but you would remove users that are active – quest4truth May 09 '16 at 18:11
  • 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." how to delete emails that are older than 4 months in the EmailID_tbl? – John Henley May 09 '16 at 18:12
  • 2
    This question does not show any research effort. – Tab Alleman May 09 '16 at 18:14
  • Please read [this](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) for some tips on improving your question. – HABO May 09 '16 at 18:16
  • I'd like to suggest you update your question and include the TSQL you've attempted, and which failed to produce the desired results. – Kennah May 09 '16 at 20:59

2 Answers2

1

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())
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
1

Here's a query that will do it:

DELETE
FROM EmailID_tbl
WHERE LastLoginDate <= DATEADD(mm, -4, GETDATE())

Change LastLoginDate to the actual name within your table which indicates the last login date, and change <= to >= if you actually mean to delete more active users instead of more inactive users.

quest4truth
  • 1,111
  • 9
  • 14