Need help with t-sql statement

Posted on 2011-04-22
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?
Question by:rutledgj
    LVL 32

    Accepted Solution


    declare @Result int

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

    select @Result

    LVL 9

    Assisted Solution

    IF EXISTS((Select count(LEFT(HierarchicalCode,2))
       FROM RaceCDC WHERE Concept = 'Payson Apache')
    (Select LEFT(HierarchicalCode,2)
       FROM RaceCDC WHERE Concept = 'Payson Apache')
    Select null;

    Please check the syntax, Not sure.

    Author Closing Comment

    Both of these work equally

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
    So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    729 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

    21 Experts available now in Live!

    Get 1:1 Help Now