📜  nullif postgresql (1)

📅  最后修改于: 2023-12-03 15:33:13.777000             🧑  作者: Mango

Introduction to NULLIF in PostgreSQL

What is NULLIF?

NULLIF is a built-in PostgreSQL function that takes two arguments and returns NULL if they are equal, otherwise it returns the first argument. The purpose of NULLIF is to simplify expressions that would otherwise need to use a CASE statement.

Syntax

The syntax for the NULLIF function is as follows:

NULLIF(expression1, expression2)

The two expressions can be of any data type, but they must be compatible with each other.

Examples

Here are some examples of using NULLIF:

SELECT NULLIF(1, 1); -- Returns NULL because 1 equals 1
SELECT NULLIF(1, 2); -- Returns 1 because 1 does not equal 2
SELECT NULLIF('hello', 'world'); -- Returns 'hello' because 'hello' does not equal 'world'
SELECT NULLIF('yes', 'yes'); -- Returns NULL because 'yes' equals 'yes'
Conclusion

The NULLIF function is a useful tool for simplifying expressions in PostgreSQL. It can help reduce the need for verbose CASE statements and make SQL code more readable.