在簡單MS SQL查詢IAM無法得到結果SQL和不取,或不給予正確的結果
SELECT * FROM tblActivityTracker
WHERE (WoM IN (NULL, 2))
AND (DateName = DATENAME(weekday, GETDATE()))
PLS建議
在簡單MS SQL查詢IAM無法得到結果SQL和不取,或不給予正確的結果
SELECT * FROM tblActivityTracker
WHERE (WoM IN (NULL, 2))
AND (DateName = DATENAME(weekday, GETDATE()))
PLS建議
你不能IN
子句中使用null
。
查詢
SELECT * FROM tblActivityTracker
WHERE (WoM IN (2) OR Wom is null) -- or Wom = 2 instead of using IN
AND (DateName = DATENAME(weekday, GETDATE()));
以上查詢提取只WoM是空的不是WoM是2 – user3926897
@ user3926897:結果將與兩個Wom有空值和2 – Wanderer
我想知道爲什麼它不提取,如果單獨運行其未來,但我需要結合 – user3926897
試試這個
SELECT * FROM tblActivityTracker
WHERE (WoM =2 OR Wom IS NULL)
AND (DateName = DATENAME(weekday, GETDATE()));
或者試試這個
SELECT * FROM tblActivityTracker
WHERE (WoM in('NULL','2'))
AND (DateName = DATENAME(weekday, GETDATE()));
你能做的最好的是使用ISNULL
SELECT * FROM tblActivityTracker
WHERE isnull(WoM,'') IN ('', '2')
AND DateName = DATENAME(weekday, GETDATE())
個
感謝的建議...... 其實下面的查詢幫助
SELECT * FROM tblActivityTracker
WHERE (WoM IN (2)) AND (DATENAME(weekday, GETDATE()) = DateName) OR
(WoM IS NULL) AND (DATENAME(weekday, GETDATE()) = DateName)
請查看示例數據和預期的結果,你會得到 – Jens
您不能使用'null'用'IN'條款的結果。 – Wanderer
如果WoM是一個字符串,使用'WHERE(WoM is NULL or WoM = 2)' –