IIF is a new inline conditional statement in SQL Server. We can pass an expression that can be evaluated to either true or false to the function and it returns one value for true and another one for false.
Let us understand this with an example:
DECLARE @MyTeam nvarchar(40) = 'Manchester United' DECLARE @YourTeam nvarchar(40) = 'Arsenal' SELECT IIF (@MyTeam = @YourTeam, 'True Devils', 'False Gooners') AS YouAre
The output of the above as below. In this we have passed two different values.
Now let us pass two same values and see how this works:
DECLARE @MyTeam nvarchar(40) = 'Manchester United' DECLARE @YourTeam nvarchar(40) = 'Manchester United' SELECT IIF (@MyTeam = @YourTeam, 'True Devils', 'False Gooners') AS YouAre
IIF is very useful where a straightforward comparison has to be made without writing case statements.
Tagged: Database, IIF, Inline statements, Languages, Microsoft SQL Server, Programming, SQL, SQL Server Management Studio
Leave a Reply