Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

T-SQL IIF

$
0
0

T-SQL IIF
Recently, I’ve decided to try to catch up on some things that were “snuck” into SQL Server while I wasn’t paying attention. Thefirst post in this series was about TRY_CONVERT() (which is awesome) and today I want to talk about IIF().

I can’t imagine that there is a single beginner who has written any appreciable amount of SQL queries who at some point didn’t have to Google “how to do an if statement in sql” or ask someone with a little more experience. I certainly know that I had to do that. What you end up with is the CASE statement.

Let’s create a table to work with and populate it with some data.

USEtempdb GO IF OBJECT_ID('Customer') IS NULL BEGIN CREATETABLEdbo.Customer ( CustomerIdINT IDENTITY(1,1) PRIMARYKEYCLUSTERED, NameNVARCHAR(100) NOTNULL, CreditLimitMONEYNOTNULL, ActiveBITNOTNULL ); INSERTdbo.Customer (Name, CreditLimit, Active) SELECT 'Nike', 10000000.00, 1 UNION SELECT 'Under Armor', 5000000.00, 1 UNION SELECT 'Reebok', 1000000.00, 1 UNION SELECT 'Buster Brown', 10000.00, 0 END GO

If we select out all of the data from the table now, it should look like this:


T-SQL IIF

So, if we were to run a standard CASE statement against the table, it might look something like this. This statement returns the name of all of the customers along with their “Credit Status”. If a credit limit is greater than 1,000,000, then they are Preferred. Otherwise, they are Standard.

SELECTName, CASE WHENCreditLimit > 1000000.00 THEN 'Preferred' ELSE 'Standard' ENDAS CreditStatus FROMdbo.Customer

This returns the following results.


T-SQL IIF

T-SQL finally has an if statement of sorts for use in SELECT statements (logical branching IFs have always existed… I used one in my table create script). Those of you with any kind of VBA background (Access, Excel Macros and Formulas, etc) are probably already familiar with IIF(), which stands for “Immediate IF”. The T-SQL version takes three parameters, just as the VBA version does. The first parameter is the conditional, the second is what will display if the conditional is true, and the third is what will display if the conditional is false. Here is that same Credit Status query rewritten to use IIF().

SELECTName, IIF(CreditLimit > 1000000.00, 'Preferred', 'Standard') AS CreditStatus FROMdbo.Customer

You can see that the results are the same and the query does look a little cleaner.


T-SQL IIF

What if you want to do multiple “layers” of the conditional? Nested IIF statements! Take a look at this query:

SELECTName, IIF(CreditLimit > 5000000.00, 'Platinum', IIF(CreditLimit >= 1000000.00, 'Gold', 'Silver')) AS CreditStatus FROMdbo.Customer

The equivalent CASE statement would look like this:

SELECTName, CASE WHENCreditLimit > 5000000.00 THEN 'Platinum' WHENCreditLimit >= 1000000.00 THEN 'Gold' ELSE 'Silver' ENDAS CreditStatus FROMdbo.Customer

Both of those return a result set like this:


T-SQL IIF

So, those statements do the same thing. For complicated conditional logic (especially logic that requires 5 or more decisions), I’d much prefer the CASE statement. Nested IIFs gets pretty dirty once you get that far. However, that appears to be an entirely stylistic choice. If you have any strong preference one way or the other, comment and let me know how you feel and why.


Viewing all articles
Browse latest Browse all 3160

Trending Articles