2014-11-22 146 views
0

我有一個簡單的表。SQL Server使用like關鍵字選擇DateTime

Booking Table

我想下火在此表上選擇查詢。

select * 
from booking 
where bookingdate like '%11%' 

在輸出中我希望顯示兩個記錄。但是,令我驚訝的是隻顯示了表中的最高記錄。

這種行爲很奇怪。 like關鍵字在DateTime列的類型上是否無法正常運行?

編輯:

但是如果我試試這個(轉換DateTimeVarchar

select * 
from booking 
where convert(varchar(50), BookingDate, 120) like '%11%' 

它的作品!

+0

什麼'DATETIME'在SQL Server是*** ***不存儲爲* *串**!它存儲爲一個8字節的二進制值 - 因此,你不能只使用'LIKE'對付它,顯然...... – 2014-11-22 20:26:47

回答

1

有像運營商對DATETIME 變量

如你所說

select * from booking 
where convert(varchar(50), BookingDate, 120) like '%11%' 

在這裏如果你想檢查與月再使用DatePart你可以將它轉換爲varchar沒有直接支持

select * from booking 
where DATEPART(M,BookingDate)=11 

OR

你可以使用日期部分,DATENAME等內置功能來獲取你需要

CHECK HERE

+0

感謝您的建議! – 2014-11-22 19:48:51

+0

@NileshBarai你的歡迎 – 2014-11-22 19:50:03