Avatar of HLRosenberger
HLRosenberger
Flag for United States of America asked on

Help with SQL joins

I've inherited a database where the creator seems to not like using NULLs.    There are text fields that cannot be NULLs and instead are set to blank.   I need to join on these fields.   I need to join between two tables where there are records that have blank for a value.  I do not want the records that are returned where they are blank.  A normal JOIN will match on there unwanted blank data values.  How can I do  JOIN to get around this?
Microsoft SQL ServerMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
HLRosenberger

8/22/2022 - Mon
Lee

Use nullif() on the join
Jim Horn

Just add an extra condition?
SELECT b.* 
FROM burgerjoints b
   JOIN recordstores r ON b.id = r.id AND ISNULL(b.id, '') <> '' 

Open in new window

Pawan Kumar

Use this

SELECT b.* 
FROM burgerjoints b
   JOIN recordstores r ON ISNULL(b.id, -999) = ISNULL(r.id,-999)

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
HLRosenberger

ASKER
I have never used nullif. How is it used on a JOIN?
Pawan Kumar

Try this

SELECT b.* 
FROM burgerjoints b
   JOIN recordstores r ON ISNULL(b.id, -999) = ISNULL(r.id,-999)

Open in new window

HLRosenberger

ASKER
Is it as simple as this.  This seems to work.

INNER JOIN dbo.DEPREC ON dbo.calibration_test.instrument = dbo.DEPREC.dep_ser and calibration_test.instrument <> ''
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jim Horn

Scott is correct, forgot about that, so I'm going to amend my first comment.  
Since there are no NULLs in the column per stated requirements, no need to handle them.
SELECT b.* 
FROM burgerjoints b
   JOIN recordstores r ON b.id = r.id AND b.id <> ''

Open in new window

HLRosenberger

ASKER
Thanks!!!