New tsql commands in sql server 2012 (Part 4)

Today i will write about two new tsql commands. The first one is choose command. The second one is the iif command. Here is an example of an query that uses choose and iif. The choose command takes the first parameter wich is an integer evaluates this integer and chooses the corresponding parameter from the parameter list. If you use choose(2,’one’,’two’,’three’) the result is ‘two’. The iif command is simular to the case command but easier to write and it has only one condition. When you write the case command case when (id <= 4) then 'first' else 'second' end you also could write iif(id <= 4, 'first', 'second').

select top 6 
        case when (id <= 4) then 'small' else 'big' end as [case],
        iif(id <= 4, 'small', 'big') as iif,
        choose(id, 'first', 'second', 'third', 'fourth', 'fifth') as choose

That is the result:


If you have a note or a question please write a comment.

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.