jase mhi
jase mhi

Reputation: 83

How to remove blank rows from SQL result set

I have a query like this:

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

My first line is blank, which causes an error in the code. My current result set looks like this:

enter image description here

Upvotes: 4

Views: 28224

Answers (2)

shA.t
shA.t

Reputation: 16978

I can suggest a trick for mixing IS NOT NULL AND <> '' like this:

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

Upvotes: 2

Mureinik
Mureinik

Reputation: 312309

In SQL Server, a null and an empty string ('') are not the same. If you which to exclude both, you should explicitly check for both:

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]

Upvotes: 6

Related Questions