【发布时间】:2020-08-31 15:17:53
【问题描述】:
我需要一个从上个月的第一天和当天的 0 小时过滤的查询。
谁能帮帮我?
【问题讨论】:
标签: sql sql-server datetime where-clause
我需要一个从上个月的第一天和当天的 0 小时过滤的查询。
谁能帮帮我?
【问题讨论】:
标签: sql sql-server datetime where-clause
使用日期算术:
where mydate >= dateadd(month, -1, datefromparts(year(getdate()),month(getdate()), 1))
表达式datefromparts(year(getdate()),month(getdate()), 1) 给出当月的第一天;然后你可以减去 1 个月来得到你想要的结果。
如果您想过滤掉今天(及以后)的数据,那么:
where
mydate >= dateadd(month, -1, datefromparts(year(getdate()),month(getdate()), 1))
and mydate < cast(getdate() as date)
【讨论】: