mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Determining The Age Of Things 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/).
|
||||||
|
|
||||||
_415 TILs and counting..._
|
_416 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -191,6 +191,7 @@ _415 TILs and counting..._
|
|||||||
- [Day Of Week For A Date](postgres/day-of-week-for-a-date.md)
|
- [Day Of Week For A Date](postgres/day-of-week-for-a-date.md)
|
||||||
- [Default Schema](postgres/default-schema.md)
|
- [Default Schema](postgres/default-schema.md)
|
||||||
- [Defining Arrays](postgres/defining-arrays.md)
|
- [Defining Arrays](postgres/defining-arrays.md)
|
||||||
|
- [Determining The Age Of Things](postgres/determining-the-age-of-things.md)
|
||||||
- [Dump And Restore A Database](postgres/dump-and-restore-a-database.md)
|
- [Dump And Restore A Database](postgres/dump-and-restore-a-database.md)
|
||||||
- [Edit Existing Functions](postgres/edit-existing-functions.md)
|
- [Edit Existing Functions](postgres/edit-existing-functions.md)
|
||||||
- [Escaping A Quote In A String](postgres/escaping-a-quote-in-a-string.md)
|
- [Escaping A Quote In A String](postgres/escaping-a-quote-in-a-string.md)
|
||||||
|
|||||||
27
postgres/determining-the-age-of-things.md
Normal file
27
postgres/determining-the-age-of-things.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Determining The Age Of Things
|
||||||
|
|
||||||
|
In PostgreSQL, we can determine the age of something (or someone) by passing
|
||||||
|
a timestamp to the `age` function.
|
||||||
|
|
||||||
|
For instance, if we want to know how long it has been since y2k, we can run
|
||||||
|
the following query:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select age(timestamp '2000-01-01');
|
||||||
|
age
|
||||||
|
-------------------------
|
||||||
|
16 years 4 mons 12 days
|
||||||
|
```
|
||||||
|
|
||||||
|
Additionally, if we want to know the amount of time between two dates, we
|
||||||
|
can pass two timestamps to the `age` function.
|
||||||
|
|
||||||
|
For example, we can find out how old Prince lived to be by passing in the
|
||||||
|
date of death and then date of birth:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select age(timestamp 'April 21, 2016', timestamp 'June 7, 1958');
|
||||||
|
age
|
||||||
|
--------------------------
|
||||||
|
57 years 10 mons 14 days
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user