?
Solved

Need help with t-sql statement

Posted on 2011-04-22
3
Medium Priority
?
161 Views
Last Modified: 2012-05-11
I have the following query

  select UID FROM RaceCDC WHERE HierarchicalCode =
  (Select LEFT(HierarchicalCode,2)
   FROM RaceCDC WHERE Concept = 'Payson Apache')

What I want to do is to return NULL when the second select fails to find a match. So if 'Payson Apache' isn't in the table I want the complete statement to return NULL.

Can someone show me how to do this?
0
Comment
Question by:rutledgj
3 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 35448126

declare @Result int

SELECT @Result = UID
FROM RaceCDC
WHERE HierarchicalCode =
  (Select LEFT(HierarchicalCode,2)
   FROM RaceCDC
   WHERE Concept = 'Payson Apache')

select @Result

0
 
LVL 9

Assisted Solution

by:radcaesar
radcaesar earned 500 total points
ID: 35448152
IF EXISTS((Select count(LEFT(HierarchicalCode,2))
   FROM RaceCDC WHERE Concept = 'Payson Apache')
(Select LEFT(HierarchicalCode,2)
   FROM RaceCDC WHERE Concept = 'Payson Apache')
ELSE
Select null;

Please check the syntax, Not sure.
0
 

Author Closing Comment

by:rutledgj
ID: 35448360
Both of these work equally
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

807 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