我在查询中有以下WHERE语句,但是如果DateOfBirth为NULL,则不会返回记录的结果。我似乎无法弄清楚,想知道是否有人可以看到原因?
WHERE
(
EXISTS( -- if we have words
SELECT *
FROM #SearchWords s
WHERE
((@SearchType = 'FirstName' OR @SearchType = 'ALL') AND c.FirstName LIKE CONCAT (s.Word,'%'))
OR ((@SearchType = 'LastName' OR @SearchType = 'ALL') AND c.LastName LIKE CONCAT(s.Word,'%'))
OR ((@SearchType = 'PreferredName' OR @SearchType = 'ALL') AND c.PreferredName LIKE CONCAT(s.Word,'%'))
OR ((@SearchType = 'Password' OR @SearchType = 'ALL') AND c.Password LIKE CONCAT(s.Word,'%'))
OR ((@SearchType = 'IdentifierValue' OR @SearchType = 'ALL') AND CI.IdentifierValue LIKE CONCAT(s.Word,'%'))
)
OR @SearchWordCount = 0 --if we don't have words
)
AND DateOfBirth BETWEEN ISNULL(@LowerDate,DateOfBirth) AND ISNULL(@UpperDate,DateOfBirth)
答案 0 :(得分:3)
您可以简单地使用OR
表达式:
AND
(DateOfBirth IS NULL OR
DateOfBirth BETWEEN COALESCE(@LowerDate, DateOfBirth) AND COALESCE(@UpperDate, DateOfBirth)
)
答案 1 :(得分:0)
可以缩短的地方
WHERE
( @SearchType in ('FirstName', 'ALL')
AND c.FirstName LIKE CONCAT (s.Word,'%') )
OR ( @SearchType in ('LastName', 'ALL')
AND c.LastName LIKE CONCAT(s.Word,'%') )
OR ( @SearchType in ('PreferredName', 'ALL')
AND c.PreferredName LIKE CONCAT(s.Word,'%') )
OR ( @SearchType in ('Password', 'ALL')
AND c.Password LIKE CONCAT(s.Word,'%'))
OR ( @SearchType in ('IdentifierValue', 'ALL')
AND CI.IdentifierValue LIKE CONCAT(s.Word,'%'))