mirror of
https://github.com/jbranchaud/til
synced 2026-01-08 01:28:02 +00:00
Add Between Symmetric as a postgres til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_503 TILs and counting..._
|
_504 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -220,6 +220,7 @@ _503 TILs and counting..._
|
|||||||
- [Aggregate A Column Into An Array](postgres/aggregate-a-column-into-an-array.md)
|
- [Aggregate A Column Into An Array](postgres/aggregate-a-column-into-an-array.md)
|
||||||
- [Assumed Radius Of The Earth](postgres/assumed-radius-of-the-earth.md)
|
- [Assumed Radius Of The Earth](postgres/assumed-radius-of-the-earth.md)
|
||||||
- [Auto Expanded Display](postgres/auto-expanded-display.md)
|
- [Auto Expanded Display](postgres/auto-expanded-display.md)
|
||||||
|
- [Between Symmetric](postgres/between-symmetric.md)
|
||||||
- [Change The Current Directory For psql](postgres/change-the-current-directory-for-psql.md)
|
- [Change The Current Directory For psql](postgres/change-the-current-directory-for-psql.md)
|
||||||
- [Checking Inequality](postgres/checking-inequality.md)
|
- [Checking Inequality](postgres/checking-inequality.md)
|
||||||
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
|
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
|
||||||
|
|||||||
41
postgres/between-symmetric.md
Normal file
41
postgres/between-symmetric.md
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# Between Symmetric
|
||||||
|
|
||||||
|
PostgreSQL's `between` construct allows you to make a comparison _between_
|
||||||
|
two values (numbers, timestamps, etc.).
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select * from generate_series(1,10) as numbers(a) where numbers.a between 3 and 6;
|
||||||
|
a
|
||||||
|
---
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
```
|
||||||
|
|
||||||
|
If you supply an empty range by using the larger of the two values first, an
|
||||||
|
empty set will result.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select * from generate_series(1,10) as numbers(a) where numbers.a between 6 and 3;
|
||||||
|
a
|
||||||
|
---
|
||||||
|
```
|
||||||
|
|
||||||
|
Tacking `symmetric` onto the `between` construct is one way to avoid this
|
||||||
|
issue.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select * from generate_series(1,10) as numbers(a) where numbers.a between symmetric 6 and 3;
|
||||||
|
a
|
||||||
|
---
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
```
|
||||||
|
|
||||||
|
> BETWEEN SYMMETRIC is the same as BETWEEN except there is no requirement
|
||||||
|
> that the argument to the left of AND be less than or equal to the argument
|
||||||
|
> on the right. If it is not, those two arguments are automatically swapped,
|
||||||
|
> so that a nonempty range is always implied.
|
||||||
Reference in New Issue
Block a user