sql – 如何获取上个月的数据和月初数据

需要帮助编写查询以获取上个月的数据以及月初至今的数据.

如果今天的日期是2011年3月23日,我需要检索上个月的数据和截至今天的数据(2011年3月23日).

如果日期是2011年4月3日,则数据应包含3月份数据和截至2011年4月3日的数据.

谢谢,

Shahsra

解决方法

Today including time info  : getdate()
Today without time info    : DATEADD(DAY,DATEDIFF(day,getdate()),0)
Tomorrow without time info : DATEADD(DAY,1)
Beginning of current month : DATEADD(month,datediff(month,0)
Beginning of last month    : DATEADD(month,getdate())-1,0)

所以最有可能

WHERE dateColumn >= DATEADD(month,0)
  AND dateColumn <  DATEADD(DAY,1)

相关文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注