Tuesday, 3 January 2017

IIF function in SQL Server

It is a SQL Server function in which you specify an expression and two possible result values,
So if the condition satisfies then it returns the first value if not then it returns the second.
You can also use it instead of CASE , in fact it is a shorthand for CASE . The query execution
plan Is also the same it just increase the readability so you might call it as  Syntactic sugar
for your code. It is familiar to ? keyword  used in c# .

Syntax:

IIF ( Boolean_expression, True_value, False_value )  


Examples:

SELECT IIF( 33 > 3,  'true' , 'false')

-- Result :  true


SELECT IIF( 2 > 3,  'true' , 'false')


-- Result :  false 


DECLARE  @passingMarks INT = 33 , @achievedMarks INT = 70
SELECT IIF(@achievedMarks >= @ passingMarks ,  'Congratulations! You have passed'  ,  'Sorry! You ditn’t make it')


-- Result :  Congratulations! You have passed




No comments:

Post a Comment