INF: Emulating the iif() Function in Transact-SQLLast reviewed: October 9, 1997Article ID: Q124243 |
The information in this article applies to:
SUMMARY
SUMMARYMicrosoft 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.
MORE INFORMATIONSome SQL implementations allow statements like the following:
select name, iif( score > 80, "Pass", "Fail" ) from examsThe iif() expression evaluates to its second argument if its first argument is true, otherwise, to its third argument. Porting code like this to Transact-SQL can be accomplished in several ways:
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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |