Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Why does this SQL bring back null?

I have a SQL statement like this

SELECT DISTINCT SiteAddress,sitecity,SiteState,SiteZip,SiteCountryCode

 FROM table
WHERE (Deleted =0
AND SiteCountryCode = 'USA')
or (SiteAddress != NULL
and SiteAddress != 'NULL')

Open in new window


SiteAddress is nvarchar(256)

Howcome this SQL brings back NULL in siteaddress?

User generated image
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

This brings back

SELECT DISTINCT SiteAddress,sitecity,SiteState,SiteZip,SiteCountryCode

 FROM table
WHERE (Deleted =0 AND SiteCountryCode = 'USA')
Or (IsNull([SiteAddress], 'NULL') <> 'Null')

Open in new window


User generated image
This seems to be correct


SELECT DISTINCT SiteAddress,sitecity,SiteState,SiteZip,SiteCountryCode

 FROM table
WHERE (Deleted =0 AND SiteCountryCode = 'USA')
and (SiteAddress IS not NULL)
---
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This should work:

SELECT DISTINCT SiteAddress ,
                sitecity ,
                SiteState ,
                SiteZip ,
                SiteCountryCode
FROM   tableName
WHERE  Deleted = 0
       AND SiteCountryCode = 'USA'
       OR SiteAddress IS NOT NULL;

Open in new window

Has to be "AND" or I'd still get null

SELECT DISTINCT SiteAddress,sitecity,SiteState,SiteZip,SiteCountryCode

 FROM table
WHERE (Deleted =0 AND SiteCountryCode = 'USA')
and (SiteAddress IS not NULL)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got it. Thanks, guys.