Solved

SQL Server: trouble with selection from derived table

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Insert to Begin if data exists 2 33
SQL Dump exec output to table 3 22
Query for timesheet application 3 18
HTML <font style="color:red"> 9 32
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

830 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