SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. IIF function

Text copied!

« Previous
Next »

IIF function

June 24, 2023, 8:27 p.m. under SQL

  • Hi! You should check DENSE_RANK function post first.

    In SQL, the IIF function helps you choose between two values based on a condition. It allows you to perform a conditional evaluation.

    It's similar to the CASE statement, with a simpler and shorter syntax for basic situations.

    The syntax of the IIF function generally looks like this :
    IIF(condition, value_if_true, value_if_false)
            

    • condition is the expression that is evaluated.

    • value_if_true is returned if the condition evaluates to true.

    • value_if_false is returned if the condition evaluates to false.

    Here's an example of how you might use the IIF function :

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

    IIF function

    2. Let's assume you want to pass students depending on their scores. If their score is more than 21, they pass; otherwise, they fail.

    3. Run below SQL statement :

    SELECT 
        Student_Name,
        Score,
        IIF(Score > 21, 'Pass', 'Fail') AS Result
    FROM Student;
            

    4. In the above statement, if the score is greater than 21 then the student is 'Pass' otherwise, 'Fail'.

    IIF function

    The IIF function in SQL is like a smart helper that helps you make decisions in your database. It checks a condition for you, and then gives you one result if the condition is true, and another if it's false. It's handy when you need to choose between different values based on some condition.

    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.

    To enhance your understanding of 'SQL functions', be sure to check out our next tutorial.

    Frequently Asked Questions :

    What does IIF do in SQL?

    The IIF function in SQL returns one of two values depending on whether the specified condition evaluates to true or false.

    What is the difference between if and IIF in SQL?

    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.

    What is the difference between IIF and case T SQL?

    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.

    Does IIF exist in SQL?

    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.
    Thank You! You should check CHOOSE function post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy