Need help with t-sql statement

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?
rutledgjAsked:
Who is Participating?
 
Ephraim WangoyaConnect With a Mentor Commented:

declare @Result int

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

select @Result

0
 
radcaesarConnect With a Mentor Commented:
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
 
rutledgjAuthor Commented:
Both of these work equally
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.