选择比今天更小的所有日期

时间:2013-11-27 08:39:33

标签: sql-server

我有一个表(mytable),其中包含:

id, date

和一些行(Id用于示例):

4235          null
3563   2013-11-27 08:02:53.917
3143   2013-11-27 01:04:51.917
1455          null
5223   2013-11-26 08:02:53.917
2123   2013-11-25 08:02:53.917

我想选择今天之前的日期或日期为空的所有行。

所以在我的例子中,如果我在2013-11-27上运行查询,我想得到:

4235          null
1455          null
5223   2013-11-26 08:02:53.917
2123   2013-11-25 08:02:53.917

我想这样做:

select case when
(
(select DATEPART(yyyy,dailybudgetexceeded) from sensplit_commercial_goal where 
commercialGoalId = 'dbe20d71-e304-4989-8524-5feef61d32a7') >= YEAR(GETDATE()) 
or...

但也许有一个更短的方式。

任何帮助表示赞赏!

3 个答案:

答案 0 :(得分:7)

select * from mytable
where [date] < Convert(date, getdate())  or [date] is null

SQLFiddle Demo

答案 1 :(得分:6)

select * 
from sensplit_commercial_goal
where commercialGoalId = 'dbe20d71-e304-4989-8524-5feef61d32a7'
and (dailybudgetexceeded < getdate() or dailybudgetexceeded is null)

答案 2 :(得分:3)

SELECT *
FROM sensplit_commercial_goal
WHERE dailybudgetexceeded  < CONVERT(date, GETDATE()) OR dailybudgetexceeded  IS NULL