mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add The nullif Function as a Postgres til
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||||
|
|
||||||
_1134 TILs and counting..._
|
_1135 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -622,6 +622,7 @@ _1134 TILs and counting..._
|
|||||||
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
|
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
|
||||||
- [Temporary Tables](postgres/temporary-tables.md)
|
- [Temporary Tables](postgres/temporary-tables.md)
|
||||||
- [Terminating A Connection](postgres/terminating-a-connection.md)
|
- [Terminating A Connection](postgres/terminating-a-connection.md)
|
||||||
|
- [The nullif Function](postgres/the-nullif-function.md)
|
||||||
- [Timestamp Functions](postgres/timestamp-functions.md)
|
- [Timestamp Functions](postgres/timestamp-functions.md)
|
||||||
- [Toggling The Pager In PSQL](postgres/toggling-the-pager-in-psql.md)
|
- [Toggling The Pager In PSQL](postgres/toggling-the-pager-in-psql.md)
|
||||||
- [Track psql History Separately Per Database](postgres/track-psql-history-separately-per-database.md)
|
- [Track psql History Separately Per Database](postgres/track-psql-history-separately-per-database.md)
|
||||||
|
|||||||
43
postgres/the-nullif-function.md
Normal file
43
postgres/the-nullif-function.md
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
# The nullif Function
|
||||||
|
|
||||||
|
PostgreSQL, in addition to generalized case statements, includes the
|
||||||
|
[`nullif`](https://www.postgresql.org/docs/current/functions-conditional.html)
|
||||||
|
function. The docs describe it as a way "to perform the inversation operation
|
||||||
|
of a `coalesce`".
|
||||||
|
|
||||||
|
Rather than resolving to some fallback value if the primary value is `null`
|
||||||
|
(like `coalesce` does), it will resolve to `null` if the given values are the
|
||||||
|
same.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select nullif(0, 0);
|
||||||
|
nullif
|
||||||
|
--------
|
||||||
|
ø
|
||||||
|
(1 row)
|
||||||
|
```
|
||||||
|
|
||||||
|
If the values are not equal, then the first value is the result of the
|
||||||
|
function.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select nullif(1, 0);
|
||||||
|
nullif
|
||||||
|
--------
|
||||||
|
1
|
||||||
|
(1 row)
|
||||||
|
```
|
||||||
|
|
||||||
|
One way this can be used is in conjunction with the `coalesce` function. For
|
||||||
|
instance, if I have a table of values that are either 0 or a positive number, I
|
||||||
|
can coerce all the zeros to be `1` like so.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select coalesce(nullif(0, 0), 1);
|
||||||
|
coalesce
|
||||||
|
----------
|
||||||
|
1
|
||||||
|
(1 row)
|
||||||
|
```
|
||||||
|
|
||||||
|
h/t [Ian Jones](https://twitter.com/_jonesian)
|
||||||
Reference in New Issue
Block a user