Solved

SQL Server: trouble with selection from derived table

Posted on 2014-01-14
4
195 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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 SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 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

15 Experts available now in Live!

Get 1:1 Help Now