New tsql commands in sql server 2012 (Part 4)Posted: 11/01/2012
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 id, data, 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 from TestData