Solved

SQL Server: trouble with selection from derived table

Posted on 2014-01-14
4
196 Views
Last Modified: 2014-01-14
Hi,

MyTable:
ID: 1
ID: 3
ID: 4

MyNames:
ID: 1   Name: Name1
ID: 2   Name: Name2
ID: 3   Name: Name3

SELECT  MyTable.ID, MyNames.Name
FROM  MyTable RIGHT OUTER JOIN
                      MyNames ON MyTable.ID = MyNames.ID

Open in new window

Result:
ID: 1   Name: Name1
ID: 3   Name: Name3
ID: 4   Name: NULL

How can I accomplish that I get the records where Name = NULL?

I've tried to add:
WHERE     (MyNames.Name IN ('', NULL))

Open in new window

But this results in zero records.
0
Comment
Question by:Delphiwizard
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39780106
Give this a whirl..

WHERE ISNULL(MyNames.Name, '') = ''
0
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 39780114
Either try

WHERE MyNames.Name IN ('') OR MyNames.Name IS NULL

If this does not work try using LEFT OUTER JOIN

SELECT  MyTable.ID, MyNames.Name
FROM  MyNames LEFT OUTER JOIN
                      MyTable ON MyTable.ID = MyNames.ID
WHERE MyNames.Name IN ('') OR MyNames.Name IS NULL
                                  

Open in new window

0
 
LVL 19

Expert Comment

by:Patricksr1972
ID: 39780115
Absolutely not the biggest SQL routined there is but

ID: 4   Name: NULL

It is the result because in Mynames you havent got id4, am i missing a point?

*edit* ah, from the query above i understand you only want to show where name is NULL reported.
0
 

Author Closing Comment

by:Delphiwizard
ID: 39780139
The first posted solution works as a charme. Thank you.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

920 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

16 Experts available now in Live!

Get 1:1 Help Now