Definition of PostgreSQL NULLIF
PostgreSQL nullif is a common conditional expression used to handle null values or expressions in PostgreSQL. nullif also used with the coalesce function to handle the null values. PostgreSQL nullif function returns a null value if provided expressions are equal. If two expressions provided are equal, then it provides a null value; as a result, otherwise, it will return the first expression as a result.
Syntax:
Below is the syntax of nullif function is as follows.
Select (Argument1 (First value which is used to handle null values), Argument2 (Second value which is used to handle null values))
SELECT Column1, …, ColumnN
COALESCE (
NULLIF (Column_name, ''), )
FROM table_name;
Parameter:
- Select: Select is used to fetch data from the table by using a nullif function in PostgreSQL. We can use multiple column or single column at one time to fetch data from the table.
- Coalesce: Coalesce states that function name in PostgreSQL, which returns as a first non-null value. Coalesce function is essential and useful in PostgreSQL.
We have used coalesce function with nullif function in PostgreSQL.
- Argument 1 to Argument 2: Argument is nothing but an integer or character value that we have passing with nullif function. If we have passing two-argument and both contain a different value, then the nullif function will return the first value in a result. If we have to pass both the same values, then it will return a null value in a result.
- Column 1 to Column N: This is the column name of the table. If we want to fetch data from a table using nullif function in PostgreSQL, we pass multiple columns simultaneously. Also, we have given the column name with the nullif function in PostgreSQL.
- From: From is a keyword in PostgreSQL used with the table name in select query.
- Table name: Table name used with nullif function to fetch data from a table.
- Nullif: It is used to handle null values in PostgreSQL; nullif also used with the coalesce function to handle the null values. nullif function returns a null value if provided expressions are equal; if provided two expressions are equal, then it provides null value; as a result, otherwise it will return the first expression as a result.
How does NULLIF Function work in PostgreSQL?
Below is the working of nullif function in PostgreSQL.
- We can use the coalesce function with nullif function in PostgreSQL. Coalesce is states that function name in PostgreSQL which returns as first non-null value as a result. Coalesce function is essential and useful in PostgreSQL.
- Nullif is a common conditional expression that was used to handle null values or expressions in PostgreSQL.
- If we have passing two nullif function arguments and the first contains a null value, then the nullif function will return the first value in a result. If we have passing both the same value, then it will return a null value in a result.
- We have used nullif function in PostgreSQL to prevent the error of division by zero.
- Nullif function is used to prevent the error of which is occurred in comparison of two values in PostgreSQL.
Examples of PostgreSQL NULLIF
Below is an example of nullif function.
- We have using a discount table to describe an example of nullif function is as follows.
- Below is the data description of the discount table, which we have used to describe an example of nullif function in PostgreSQL.
Example #1
testing=# select * from discount;
Output:
Example #2
In the below example, we have passing values like 50 and 50. Nullif function will return null values because both the arguments which we have passing are the same.
testing=# select nullif (50, 50);
Output:
In the above example, we have the passing the same argument with the nullif function so that it will return the null value as a result.
Example #3
In the below example, we have passing values as 50 and 100. Nullif function will return the first value, i.e. 50, because both the arguments which we have passing are different.
testing=# select nullif (50, 100);
Output:
In the above example, we have a passing different argument with the nullif function so that it will return the first value as a result.
Example #4
In the below example, we have passing values as A, and P. Nullif function will return first value, i.e. A, because both the arguments which we have passing are different.
testing=# select nullif (‘A’, ‘P’);
Output:
In the above example, we have a passing different argument with the nullif function so that it will return the first value as a result.
Example #5
In the below example, we have to retrieve data from a discount table using the nullif function.
testing=# SELECT cust_id, product_name, COALESCE ( NULLIF (Product_price, '')) AS Product_price FROM discount;
Output:
Recommended Articles
This is a guide to PostgreSQL NULLIF. Here we also discuss the Introduction and how nullif function works in PostgreSQL and different examples and its code implementation. You may also have a look at the following articles to learn more –