Skip to content
Advertisement

How to write SQL Server query to pull date that are coming in 3 month?

I have column that has expiration date and I would like to pull expiration date that are coming within 3 month.

WHERE DATEDIFF(month, t5.warrantyexpirydate, GETDATE()) <= 3

or

WHERE t5.warrantyexpirydate >= DATEADD(month, 2, GETDATE())

Both of these where statement pull any items that are greater than 3 months. Can anyone help me with this?

Thank you!

Advertisement

Answer

Use >= and <= logic, as it’s SARGable when you don’t apply functions to your column:

WHERE warrantyexpirydate >= DATEADD(MONTH, -3, GETDATE())
  AND warrantyexpirydate <= GETDATE() --This isn't needed if the expiry can't be in the future
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement