On this page
NULLIF
Prev: `COALESCE` | Up: functions | Next: `CASE`
Rel: functions, Aggregations, CTEs & Window Functions
Ref: PostgreSQL — SQL Commands
Tags: #public #databases #sql #functions #tier/standard
Syntax & Overview
total / NULLIF(count, 0)
Returns NULL if a = b, else returns a.
- Category / Stage: Null Handling
- Dialects: ANSI, SQLite, PostgreSQL
Notes & Best Practices
- Returns
NULLwhena = b, otherwisea: Classic zero-division guard (total / NULLIF(count, 0)) and empty-string normalizer (NULLIF(trim(s), '')).
Example
SELECT total / NULLIF(item_count, 0) AS avg_price
FROM invoices;
Linked from
Related notes
- tuts/databases/sql/functions/CASE Linked here; links to this page; shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/COALESCE Linked here; links to this page; shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions Linked here; links to this page; shared tags: #databases, #functions, #sql
- tuts/databases/sql/aggregations_ctes_and_windows Linked here; shared tags: #databases, #sql
- tuts/databases/sql/functions/CAST shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/COUNT shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/CURRENT_TIMESTAMP shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/LAG shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/LOWER shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/RANK shared tags: #databases, #functions, #sql, #tier