我有这样的查询:
SELECT DISTINCT
[F_Exhibitor_Name]
FROM
[V_ExhibitorLocation]
WHERE
F_ExhibitionCode ='10996'
AND
[F_Exhibitor_Name] IS NOT NULL
ORDER BY
F_Exhibitor_Name
我的第一行是空白,这会导致代码出错。我当前的结果集如下所示:
答案 0 :(得分:5)
在SQL Server中,null
和空字符串(''
)不相同。如果要排除这两者,则应明确检查两者:
SELECT DISTINCT [F_Exhibitor_Name]
FROM [V_ExhibitorLocation]
WHERE [F_ExhibitionCode] = '10996' AND
[F_Exhibitor_Name] IS NOT NULL AND
[F_Exhibitor_Name] <> ''
ORDER BY [F_Exhibitor_Name]
答案 1 :(得分:2)
我可以提出一个混合IS NOT NULL
和<> ''
的技巧:
SELECT DISTINCT
F_Exhibitor_Name
FROM
V_ExhibitorLocation
WHERE
F_ExhibitionCode = '10996'
AND
F_Exhibitor_Name > '' --or ISNULL(F_Exhibitor_Name, '') <> ''
ORDER BY
F_Exhibitor_Name