用于获取过去 3 个月数据的 SQL 查询

2021-12-13 00:00:00 sql date select sql-server-2008 sql-server

如何获取今天的日期并将其转换为01/mm/yyyy 格式并从表中获取3 个月前交货月份的数据?表中已包含交货月份 01/mm/yyyy.

How can you get today's date and convert it to 01/mm /yyyy format and get data from the table with delivery month 3 months ago? Table already contains delivery month as 01/mm/yyyy.

推荐答案

SELECT * 
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, -3, GETDATE()) 

Mureinik 建议的方法将返回相同的结果,但通过这种方式,您的查询可以从 Date_Column 上的任何索引中受益.

Mureinik's suggested method will return the same results, but doing it this way your query can benefit from any indexes on Date_Column.

或者您可以检查过去 90 天.

or you can check against last 90 days.

SELECT * 
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(DAY, -90, GETDATE()) 

相关文章