SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Training
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


FAQ Topics

All FAQ's
General DBA
General Developer
DBA Performance Tuning
Developer Performance Tuning
Clustering
Error Messages

USEFUL SITES :

ASP.NET Tutorials
Windows and SQL Azure Tutorials
Cloud Hosting Magazine
SharePoint Tutorials
Windows Server Help

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server

More     
 
Latest FAQ's

Add Node to A SQL Server failover Cluster failed with invalid ...
SQL Server Destination remote server error
Setting Up Data And Log Files For SQL Server
Will Check Constraints Improve Database Performance?

More     
   
Latest Software Reviews

dbForge Review
Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008

More     

How can you use IIf in Transact-SQL?



This is a quite common question. It is usually asked by people arriving at SQL Server with a background in Microsoft Access. They either want to use SQL Server as a backend for their Access project, or they are otherwise upsizing from Access to SQL Server. The answer, however, is usually not much appreciated at first:

There is no IIf in SQL Server's Transact SQL language!

Like it or not, such queries have to be rewritten using the CASE expression. Let's look at a simple example:

SELECT 
 Customers.CustomerID
 , Customers.CompanyName
 , Customers.Country
 , IIf([Country]="Germany","0049 " & [Phone],[Phone]) AS Telefon
FROM 
 Customers

This is a valid query in Access, which evaluates within Access' Northwind sample database whether a Customer is located in Germany or not. If this is the case (pun intended!), it automatically adds the international telephone number for Germany in front of the phone number. If you try to run this in SQL Server's Query Analyzer, you'll get:

Server: Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near '='.

That's it. The query stops with this error message. So, as was mentioned above, the query has to be rewritten using the CASE expression. That might look something like this:

SELECT 
 Customers.CustomerID
 , Customers.CompanyName
 , Customers.Country
 , CASE
    WHEN Country='Germany' 
     THEN '0049 ' + Phone
     ELSE Phone
     END AS Phone
FROM 
 Customers

This is a valid Transact-SQL query, which SQL Server can understand and execute.

CASE is one of the most powerful commands in the Transact-SQL language. In contrast to IIf, where you only evaluate one logical expression at a time, this limitation does not exist for CASE. Try, for example, to put this in one single IIf expression:

SELECT 
 Customers.CustomerID
 , Customers.CompanyName
 , Customers.Country
 , CASE Country
    WHEN 'Germany' 
     THEN '0049 ' + Phone
    WHEN 'Mexico'
     THEN 'Fiesta ' + Phone
    WHEN 'UK'
     THEN 'Black Pudding (Yuk!) ' + Phone
     ELSE Phone
     END AS Phone
FROM 
 Customers

Don't spent too much time here on the sense of this query, but you will get the idea of what is possible with CASE. And once you are familiar with using CASE, you'll hardly miss IIf anymore.








C# Help and Tutorials | PHP MySQL Tutorial | Sharepoint Tutorial | Azure Tutorial | Cloud Hosting Magazine | ASP.NET Tutorials | Windows Server Help | Windows Phone Pro | Silverlight Ace | Visual Studio Tutorials | Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 2010 Jude O'Kelly. All rights reserved