SQL SELECT CASE Instead of IIF
I’ve had to do this a couple of times and I just realized I haven’t written about it anywhere.
If you need to SELECT a field from a row that returns different content based on the content of the field, you’d think, based on previous programming experience, that your code would look something like this.
SELECT IIF(field=2,"content true","content false") FROM DB
But you’d be wrong.
No, the people who created SQL thought a syntax with a little richer flexibility would do the trick. Why limit ourselves to two states when multiple states will do?
SELECT CASE field WHEN 2 THEN "content true" ELSE "content false" END FROM DB
In fact you can have multiple WHEN conditions in your code
SELECT CASE field WHEN 1 THEN 'One' WHEN 2 THEN 'Two' WHEN 3 THEN 'Three' END FROM DB
You can also use multiple fields
SELECT CASE WHEN field1 = 1 THEN 'field one' WHEN field2 = 2 THEN 'field two' ELSE 'Something else' END FROM DB
Other post in SQL For Programmers
- SQL For Developers - 9 Reasons to bother - August 13th, 2008
- MSSQL CREATE and DROP Database - SQL for Programmers - August 20th, 2008
- MSSQL CREATE TABLE - SQL For Programmers - August 22nd, 2008
- SQL For Programmers - ALTERing the TABLE - September 1st, 2008
- SQL For Programmers - Finding a String - September 3rd, 2008
- SQL For Programmers - Finding IN a List - September 8th, 2008
- SQL For Programmers - Stored Procedures (Better than LINQ) - September 10th, 2008
- SQL For Programmers - Stored Procedure Basics - September 16th, 2008
- Basic SQL Commands - SQL For Programmers - September 18th, 2008
- SQL IF/WHILE Blocks - SQL For Programmers - September 30th, 2008
- SQL WHILE - SQL For Programmers - October 2nd, 2008
- Temporary Tables - SQL For Programmers - October 7th, 2008
- SQL CURSOR - SQL For Programmers - October 13th, 2008
- SQL CURSOR Performance - SQL For Programmers - October 22nd, 2008
- Random in SQL - SQL For Programmers - November 4th, 2008
- SQL - Filtering WHERE condition on two rows - November 26th, 2008
- SQL - Transactions - April 15th, 2009
- SQL For Programmers – New Question - July 6th, 2009
- SQL SELECT CASE Instead of IIF - October 20th, 2009
Other Related Items:
Who Moved My Cheese?: An Amazing Way to Deal with Change in Your Work and in Your LifeOutline Change can be a blessing or a curse, depending on your perspective. The message of Who Moved My Cheese? is that all can come to see it as a blessing, if they understand the nature of cheese a
Wilson Jones 362 Line Basic Round Ring View Binder, 3-Inch Capacity, 8.5 x 11 Inch Sheet Size, White (W362-49W)Looks good on the shelf or on the go. Customize for filing or presentations with a non-glare overlay on the spine and front and back covers. Use the round ring open and close triggers for easy access. Take advantage of interior pockets for extra storage.
Bounty Paper Towels, White, Regular Roll (Case of 30)Bounty Paper Towels, White, Regular Roll, this is one white you won't mind dirtying up. Whether it's muddy paw prints on the floor or soda spills on the table, new and improved Bounty has the strength, absorbency, and cloth-like durability to clean the mess with less.
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!










Is there a performance difference?
Yeah. IIF doesn’t perform at all. You HAVE TO use SELECT CASE if you want to do this as part of a SELECT statement in TSQL.