简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM