我在SQL Server查询中需要帮助

问题描述

我需要一个查询,该查询从上个月的第一天到今天的0小时进行过滤。

有人可以帮助我吗?

解决方法

使用日期算术:

where mydate >= dateadd(month,-1,datefromparts(year(getdate()),month(getdate()),1))

表达式datefromparts(year(getdate()),1)为您提供当月的第一天;然后可以减去1个月以获得所需的结果。

如果您想过滤掉今天(以及以后)的数据,则:

where 
    mydate >= dateadd(month,1))
    and mydate < cast(getdate() as date)