SQL > SQL NULL > IFNULL Function

The IFNULL( ) function is available in MySQL and SparkSQL, and not in SQL Server, Oracle, or HiveQL (Hive SQL). This function takes two arguments. If the first argument is not NULL, the function returns the first argument. Otherwise, the second argument is returned. This function is commonly used to replace NULL value with another value. It is similar to the NVL function in Oracle and the ISNULL Function in SQL Server.

For example, if we have the following table,

Table Sales_Data

 Store_Name  Sales 
 Store A 300 
 Store B NULL 

The following SQL,

SELECT SUM(IFNULL(Sales,100)) FROM Sales_Data;

Result:

SUM(IFNULL(Sales,100))
400

This is because NULL has been replaced by 100 via the IFNULL function. The total then becomes 300 + 100 = 400.

Next: SQL NVL Function

This page was last updated on June 19, 2023.




Copyright © 2024   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact