SQL AND没有提取,或者没有给出正确的结果

时间:2017-03-06 11:41:20

标签: sql sql-server

在简单的MS Sql Query中我无法获得结果

SELECT * FROM tblActivityTracker
WHERE (WoM IN (NULL, 2)) 
AND (DateName = DATENAME(weekday, GETDATE()))

请建议

4 个答案:

答案 0 :(得分:1)

您不能将nullIN子句一起使用。

<强>查询

SELECT * FROM tblActivityTracker
WHERE (WoM IN (2) OR Wom is null) -- or Wom = 2 instead of using IN
AND (DateName = DATENAME(weekday, GETDATE()));

答案 1 :(得分:1)

试试这个

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()));

答案 2 :(得分:1)

你能做的最好就是使用isnull

SELECT * FROM tblActivityTracker
WHERE isnull(WoM,'') IN ('', '2')
AND DateName = DATENAME(weekday, GETDATE())

答案 3 :(得分:0)

感谢您的建议...... 实际上下面的查询有帮助

SELECT * FROM tblActivityTracker
WHERE (WoM IN (2)) AND (DATENAME(weekday, GETDATE()) = DateName) OR
  (WoM IS NULL) AND (DATENAME(weekday, GETDATE()) = DateName)