| INF: Emulating the iif() Function in Transact-SQLID: Q124243 
 | 
Microsoft SQL Server version 6.5 supports the powerful ANSI SQL-92
CASE expression which allows SQL expressions to be simplified for
conditional values. For more information on how to use the CASE command,
see the Microsoft Books OnLine for SQL Server version 6.5.
Prior to SQL Server version 6.5, the Transact-SQL language did not have
an equivalent to CASE or the function iif() (sometimes called Immediate If
or Inline If) that is available in other programming languages, including
Microsoft Access and Microsoft FoxPro. This article provides information
on how to produce similar results using the Transact-SQL features
available in versions of SQL Server prior to version 6.5.
Some SQL implementations allow statements like the following:
   select name, iif( score > 80, "Pass", "Fail" ) from exams 
      select name,
        case
          when score > 80 then "Pass"
          else "Fail"
        end
      from exams
 
   score      grade
   -----      -----
   1          'Fail'
   2          'Fail'
   3          'Fail'
   ...
   99         'Pass'
   100        'Pass'
 
   select exams.name, grading.grade from exams, grading
   where exams.score = grading.score 
   select name, 'Fail' from exams where score <= 80
   union all
   select name, 'Pass' from exams where score > 80
 
   select
     name,
     SUBSTRING('FailPass', 1 + 4 * SIGN((score - 80) + ABS(score - 80)), 4)
     from exams
 Additional query words: sql6 Windows NT
Keywords          : kbprg SSrvProg SSrvTran_SQL 
Version           : 4.2 | 4.2 6.0
Platform          : OS/2 WINDOWS 
Issue type        : Last Reviewed: April 17, 1999