Skip to content
Advertisement

Use one of two dates in where clause

I need to delete all records in a table where the CreatedDate or ModifiedDate is greater than x. The logic is as follows:

  • If ModifiedDate is not null then use this value
  • Otherwise use the CreatedDate value

Not applying the if/else statement correctly.

delete * from table where
-- how to implement the following
if ModifiedDate is not null then ModifiedDate < GETDATE() - 30
else CreatedDate < GETDATE() - 30

Advertisement

Answer

Use this where clause:

where coalesce(ModifiedDate, CreateDate) < dateadd(day, -30, getdate())
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement