我正在选择客户ID,姓名和服务日期等字段。我正在尝试写我的where子句,每天我运行我的查询它将捕获当前第一个月到当天的日期范围。
答案 0 :(得分:2)
一种方法是比较月份和年份:
where year(col) = year(getdate()) and
month(col) = month(getdate()) and
day(col) <= day(getdate()) -- this is optional, if there is no future data
另一种方法是与月初进行比较。我会这样做:
where col >= cast(dateadd(day, 1 - day(getdate), getdate()) as date)
(这假设表格中没有未来的数据。)
或者,更好的是:
where col >= datefromparts(year(getdate()), month(getdate()), 1)
答案 1 :(得分:0)
Select *
From YourTable
where SomeDateField bewteen cast(DateAdd(DD,-Day(GetDate())+1,GetDate()) as Date) and cast(getDate() as Date)
and ...
编辑:我使用BETWEEN来捕获可能的未来日期/预定事件