Select statement

  • I currently have a select where for one of the fields I need to change the output if it is null or contains the code 'v918899' For eveything else just display the contents. Effectively the logic I want is:

    If code is null then change to '5K5'

    If code is = 'V918899' THEN CHANGE TO '5k5'

    ELSE display code.

    I have tried to use a case statement but it dosen't seem to work. ANy ideas?

  • eseosaoregie (12/3/2008)


    I currently have a select where for one of the fields I need to change the output if it is null or contains the code 'v918899' For eveything else just display the contents. Effectively the logic I want is:

    If code is null then change to '5K5'

    If code is = 'V918899' THEN CHANGE TO '5k5'

    ELSE display code.

    I have tried to use a case statement but it dosen't seem to work. ANy ideas?

    You need a searched CASE like this:

    CASE WHEN code IS NULL OR code = 'V918899' THEN '5K5' ELSE code END

    Cheers

    ChrisM

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Try this...

    select

    case

    when Code= 'V918899' or Code IS NULL then '5K5'

    else Code

    end

    from YourTable

    -----------------------------------------------
    SQL Server posts at my blog, Grapefruitmoon.Net[/url]

  • Cheers that works.

  • Try this alternate coding ...

    Its only a way of rewriting existing formats suggested by other learned members from the forum :-

    CASE WHEN ISNULL([FieldName], '5K5') = 'V918899'

    THEN '5K5'

    ELSE ISNULL([FieldName], '5K5')

    END CASE

  • One more way: COALESCE(NULLIF(code,'V918899'), '5K5')

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply