需要帮助编写查询以获取上个月的数据以及本月至今的数据。
如果今天的日期是2011年3月23日,我需要检索上个月的数据以及直到今天的数据(指2011年3月23日)。
如果日期是 2011 年 4 月 3 日,则数据应包含 3 月数据和截至 2011 年 4 月 3 日的数据。
谢谢
沙赫斯拉
最佳答案
Today including time info : getdate()
Today without time info : DATEADD(DAY, DATEDIFF(day, 0, getdate()), 0)
Tomorrow without time info : DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)
Beginning of current month : DATEADD(month, datediff(month, 0, getdate()), 0)
Beginning of last month : DATEADD(month, datediff(month, 0, getdate())-1, 0)
很有可能
WHERE dateColumn >= DATEADD(month, datediff(month, 0, getdate())-1, 0)
AND dateColumn < DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)
关于sql - 如何获取上个月数据和本月至今数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5412019/