IIF function | SQL Tutorial and Query Example

Text copied!

IIF function


  • The syntax of the IIF function generally looks like this :
    Here's an example of how you might use the IIF function :

    1. Let's assume we have a table named "[Student]".

    Remember :

    Using this function in a select statement won't modify the [Student] table directly, but it will only be reflected in the select statement's output.

    IIF(condition, value_if_true, value_if_false)
            
    To enhance your understanding of 'SQL functions', be sure to check out our next tutorial.
    IIF function
    SELECT 
        Student_Name,
        Score,
        IIF(Score > 21, 'Pass', 'Fail') AS Result
    FROM Student;
            
    IIF function

    Frequently Asked Questions :

    The IIF function in SQL returns one of two values depending on whether the specified condition evaluates to true or false.
    The difference between IF and IIF in SQL is that IF is used for flow control within procedural code (like stored procedures), while IIF is a scalar function used to return a value based on a condition.
    The difference between IIF and CASE in T-SQL is that CASE is more flexible and allows for multiple conditions and branches, while IIF is simpler and only handles a single condition.
    IIF does exist in SQL, but its availability depends on the specific database management system (DBMS) being used. It's commonly supported in systems like Microsoft SQL Server and Microsoft Access.