Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Use nullif() in PostgreSQL
The NULLIF()
function in PostgreSQL is a powerful tool used to handle null values in your queries. It compares two expressions and returns NULL
if they are equal; otherwise, it returns the first expression.
Syntax of nullif()
The basic syntax of the NULLIF()
function is as follows:
NULLIF(expression1, expression2)
Where expression1
and expression2
are the two values being compared. If they are equal, the function returns NULL
; if they are different, it returns the value of expression1
.
Examples
Example 1: Basic nullif() Usage
Let's say you have a table employees
with the columns salary
and bonus
. You can use NULLIF()
to avoid returning a salary value of 0
:
SELECT employee_id, salary, bonus,
NULLIF(salary, 0) AS salary_non_zero
FROM employees;
In this example, if salary
is equal to 0
, NULLIF()
will return NULL
instead of 0
.
Example 2: Comparing Two Columns
If you want to check if two columns have the same value, you can use NULLIF()
to return NULL
if they are equal:
SELECT employee_id, department, role,
NULLIF(department, role) AS different_dept_role
FROM employees;
Here, if the department
and role
are the same, the function will return NULL
; otherwise, it will return the value of department
.
Why Use NULLIF?
The NULLIF()
function is especially useful for:
- Avoiding division by zero errors in queries (when using it with arithmetic operations).
- Handling edge cases where specific values are problematic and you want to replace them with
NULL
. - Creating more readable and cleaner SQL queries without the need for complex
CASE
statements.
Conclusion
The NULLIF()
function in PostgreSQL is a simple yet effective way to handle edge cases where two expressions may be equal. By using it correctly, you can clean up your data and make your queries more efficient and readable.