Solved

Help with SQL joins

Posted on 2016-09-29
9
61 Views
Last Modified: 2016-10-05
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?
0
Comment
Question by:HLRosenberger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 41821903
Use nullif() on the join
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41821907
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

0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41821919
Use this

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

Open in new window

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 1

Author Comment

by:HLRosenberger
ID: 41821922
I have never used nullif. How is it used on a JOIN?
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41821925
Try this

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

Open in new window

0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 41821937
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 <> ''
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 41822124
Do not use any function in any JOIN unless it's absolutely unavoidable, because it makes the comparison "non-sargable".  And ISNULL and NULLIF are always avoidable.

INNER JOIN dbo.DEPREC ON
    dbo.calibration_test.instrument > '' AND
    dbo.calibration_test.instrument = dbo.DEPREC.dep_ser
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41822146
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

0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 41830415
Thanks!!!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question