如何从SQL结果集中删除空行

时间:2015-05-17 07:39:37

标签: sql sql-server select null

我有这样的查询:

SELECT DISTINCT 
    [F_Exhibitor_Name] 
FROM 
    [V_ExhibitorLocation] 
WHERE 
    F_ExhibitionCode ='10996' 
  AND 
    [F_Exhibitor_Name] IS NOT NULL
ORDER BY 
    F_Exhibitor_Name

我的第一行是空白,这会导致代码出错。我当前的结果集如下所示:

enter image description here

2 个答案:

答案 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