?
Solved

SQL Server: trouble with selection from derived table

Posted on 2014-01-14
4
Medium Priority
?
202 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
[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
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 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 23

Expert Comment

by:Patrick Bogers
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.
Suggested Courses

777 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