?
Solved

Help with SQL joins

Posted on 2016-09-29
9
Medium Priority
?
75 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
  • 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 37

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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 1

Author Comment

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

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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

862 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