2013-11-27 213 views
5

比更小的我有一個表(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... 

但也許有一個較短的方式。

任何幫助讚賞!

+0

你今天* *前平均日期? –

回答

5
select * 
from sensplit_commercial_goal 
where commercialGoalId = 'dbe20d71-e304-4989-8524-5feef61d32a7' 
and (dailybudgetexceeded < getdate() or dailybudgetexceeded is null) 
6
select * from mytable 
where [date] < Convert(date, getdate()) or [date] is null 

SQLFiddle Demo

+0

爲什麼要將GetDate()轉換爲日期? –

+1

如果查詢在2013-11-27 08:02:54.917上運行,則2013-11-27 08:02:53.917的記錄也將由查詢返回,除非您將GetDate()轉換爲日期 – Raj

+0

OP沒有要求截斷時間 - 至少不明確 –

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

爲什麼要將GetDate()轉換爲Date? –

+0

截斷日期時間的時間部分。 – kingkong0924

相關問題