Solved

Help with SQL joins

Posted on 2016-09-29
9
47 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 65

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 28

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 1

Author Comment

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

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 65

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 59
How to share SSIS Package? 6 35
SQL Server - Set Value of Multiple Fields in One Query 10 22
T-SQL: Do I need CLUSTERED here? 13 37
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now