IF / ELSE and REPLACE in view creation?

Posted on 2011-10-23
Last Modified: 2012-05-12
I am creating a view and would like to display something like the following...

If field equals XXX then print 'FOO', else print 'BAR'

... as a new field in the view.

New to T-SQL, thanks in advance for any pointers...
Question by:AndyWF
    LVL 50

    Accepted Solution

    the clause you are looking for is the case statement...

    select  case when field = 'xxx' then foo else bar end as mycolumnname
    from ....

    two forms are available for the case statement...
    case when expression = expression then expression
            when expression1 condition expression2 then expression3
            else expression4


    case expression
            when value/expression   then expression
            when ....
            else expression

    Author Closing Comment

    Thank you!  That got me exactly where I needed to be...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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!

    Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
    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…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    759 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

    12 Experts available now in Live!

    Get 1:1 Help Now