Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server INLINE IF ELSE

In my table I got a column whose value is whether 0 or 1. If that column is 0 I output the value as 'no'; if 1 I should output as 'yes' for all rows. How can I do this only using SQL statement. Thanks

like image 534
Alaattin KAYRAK Avatar asked Jul 07 '11 08:07

Alaattin KAYRAK


1 Answers

I understand that this question (which shows up at the top of google results for "sql server inline if") is 2 years old, but with SQL Server 2012, the answers are somewhat outdated. It also appears to be a duplicate of SQL inline if statement type question, but that question (being an even older one), doesn't have an up to date answer either.

In SQL Server 2012 you can use the IIF function:

IIF ( boolean_expression, true_value, false_value ) 

Example:

SELECT IIF(someColumn = 1, 'yes', 'no') 
like image 92
jahu Avatar answered Oct 13 '22 16:10

jahu