SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stairway to T-SQL: Beyond The Basics Level 6: Using the CASE Expression and IIF Function


Stairway to T-SQL: Beyond The Basics Level 6: Using the CASE Expression and IIF Function

Author
Message
Greg Larsen
Greg Larsen
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6847 Visits: 290
Comments posted to this topic are about the item Stairway to T-SQL: Beyond The Basics Level 6: Using the CASE Expression and IIF Function

Gregory A. Larsen, MVP
Tee Time
Tee Time
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1489 Visits: 465
Thanks Greg, good stuff! I like the addition of IIF in 2012. :-)
jagsalog
jagsalog
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 200
Thank you, very helpful.

In a large development, I would use a only on CASE rather than IIF for readability and uniformity.
Mike Dougherty-384281
Mike Dougherty-384281
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1126 Visits: 944
I was surprised you didn't discuss case inside aggregate functions.

before PIVOT existed we used something like this:
select
entityid
,fieldA = max( case when attribute="A" then value else '' end )
,fieldB = max( case when attribute="B" then value else '' end )
,...
,fieldN = max( case when attribute="N" then value else '' end )
from
EAVtable
group by
entityid

I've also used case for custom group/order by:
order by
case @tcSortField
when "LastName" then person.LastName
when "Email" then person.EmailAddress
etc.
end
webrunner
webrunner
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11793 Visits: 4051
Thanks for the great article!

I just want to make one small comment. I ran the code below:

SELECT OrderAmt,
IIF(OrderAmt > 200,
'High $ Order',
'Low $ Order') AS OrderType
FROM MyOrder;


and got this error:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '>'.

When I Googled the error, it turns out IIF() is not implemented in SQL 2008. I didn't see that noted in the article so it would be helpful to add a note about that at the start of the piece.

But again, this is a fantastic tutorial! Thanks again!

- webrunner

-------------------
"I love spending twice as long and working twice as hard to get half as much done!" – Nobody ever.
Ref.: http://www.adminarsenal.com/admin-arsenal-blog/powershell-how-to-write-your-first-powershell-script

"Operator! Give me the number for 911!" - Homer Simpson

"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
msparks 6239
msparks 6239
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 43
Hello,
In your Stairway to T-SQL Beyond The Basics Level 6 Using the CASE Expression and IIF Function covering the Listing 7: Nesting CASE statement lesson. If I was to add to your “MyOrder” table the values;
('01-15-2015', 301.00,NULL), and then run your code, the result for my added value would return. Meaning that if the OrderAmt_Category is Null it would be a layaway. I would think that it should be without layaway.

301.00 NULL 300 Dollar Order with Layaway
USE tempdb
GO
SELECT OrderAmt, Layaway,
CASE
WHEN OrderAmt < 100 THEN '< 100 Dollar Order'
WHEN OrderAmt < 200 THEN '100 Dollar Order'
WHEN OrderAmt < 300 THEN
CASE
WHEN Layaway = 'N'
THEN '200 Dollar Order without Layaway'
ELSE '200 Dollar Order with Layaway' END
ELSE
CASE
WHEN Layaway = 'N'
THEN '300 Dollar Order without Layaway'
ELSE '300 Dollar Order with Layaway' END
END AS OrderAmt_Category
FROM MyOrder
order by OrderAmt;

-----------Reorder/Change the 2nd and 3rd CASE statements to accomodate the "Null" values correctly
USE tempdb
GO
SELECT OrderAmt, Layaway,
CASE
WHEN OrderAmt < 100 THEN '< 100 Dollar Order'
WHEN OrderAmt < 200 THEN '100 Dollar Order'
WHEN OrderAmt < 300 THEN
CASE
WHEN Layaway = 'Y'
THEN '200 Dollar Order with Layaway'
ELSE '200 Dollar Order without Layaway' END
ELSE
CASE
WHEN Layaway = 'Y'
THEN '300 Dollar Order with Layaway'
ELSE '300 Dollar Order without Layaway' END
END AS OrderAmt_Category
FROM MyOrder
order by OrderAmt;

Great article and very helpful
kwoznica
kwoznica
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1947 Visits: 481
Nice article Greg.

I've had to use CASE expressions quite often recently in a data migration from an old ERP system.

You not only just taught me a few new tricks but answered lingering questions I had on why some results are returned.
Iwas Bornready
Iwas Bornready
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29472 Visits: 885
Good basic info about CASE. I've never used IIF, always just CASE.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search