JournalSQLFunctions and dates2 min
DATE_TRUNC() vs. DATE_PART()
DATE_TRUNC allows you to truncate your date to a particular part of your date-time column. Common trunctions are day, month, and year.
- DATE_TRUNC
- DATE_PART
- month
- year
Based on Lesson 6.26 Video: DATE Function II:
DATE_TRUNC allows you to truncate your date to a particular part of your date-time column. Common trunctions are day, month, and year.
DATE_PART can be useful for pulling a specific portion of a date, but notice pulling month or day of the week (dow) means that you are no longer keeping the years in order. Rather you are grouping for certain components regardless of which year they belonged in.
DATE_TRUNC() Function
Syntax
date_trunc('datepart', field)The datepart argument is the level of precision used to truncate the field, which can be one of the following:
- millennium
- century
- decade
- year
- quarter
- month
- week
- day
- hour
- minute
- second
- milliseconds
- microseconds
Example
See the following rental table:
You can count the number of rentals by month by using the date_trunc function as follows:
SELECT
date_trunc('month', rental_date) m,
COUNT (rental_id)
FROM
rental
GROUP BY
m
ORDER BY
m;In this query, the date_trunc function truncates the rental date to month part. The COUNT function counts the number of rentals and the GROUP BY clause groups the rentals by month.
month | count
---------------------+-------
2005-05-01 00:00:00 | 1156
2005-06-01 00:00:00 | 2311
2005-07-01 00:00:00 | 6709
2005-08-01 00:00:00 | 5686
2006-02-01 00:00:00 | 182
(5 rows)Similarly, you can count the number of rentals by staff per year as follows:
SELECT
staff_id,
date_trunc('year', rental_date) y,
COUNT (rental_id) rental
FROM
rental
GROUP BY
staff_id, y
ORDER BY
staff_idThe following shows the output:
staff_id | y | rental
----------+---------------------+--------
1 | 2006-01-01 00:00:00 | 85
1 | 2005-01-01 00:00:00 | 7955
2 | 2005-01-01 00:00:00 | 7907
2 | 2006-01-01 00:00:00 | 97
(4 rows)DATE_PART() Function
Syntax
DATE_PART(field,source)The field is an identifier that determines what field to extract from the source. The values of the field must be in a list of permitted values mentioned below:
- century
- decade
- year
- month
- day
- hour
- minute
- second
- microseconds
- milliseconds
- dow
- doy
- epoch
- isodow
- isoyear
- timezone
- timezone_hour
- timezone_minute
Example
The following example extracts the century from a time stamp:
SELECT date_part('century',TIMESTAMP '2017-01-01');
date_part
-----------
21
(1 row)To extract the year from the same timestamp, you pass the year to the field argument:
SELECT date_part('year',TIMESTAMP '2017-01-01');
date_part
-----------
2017
(1 row)To extract the quarter, you use the following statement:
SELECT date_part('quarter',TIMESTAMP '2017-01-01');
date_part
-----------
1
(1 row)To get the month, you pass the month to the DATE_PART() function:
SELECT date_part('month',TIMESTAMP '2017-09-30');
date_part
-----------
9
(1 row)To extract the week number from a time stamp, you pass the week as the first argument:
SELECT date_part('week',TIMESTAMP '2017-09-30');
date_part
-----------
39
(1 row)To extract the hour, minute, second from a time stamp, you pass the corresponding value hour, minute and second to the DATE_PART() function:
SELECT date_part('hour',TIMESTAMP '2017-03-18 10:20:30') h,
date_part('minute',TIMESTAMP '2017-03-18 10:20:30') m,
date_part('second',TIMESTAMP '2017-03-18 10:20:30') s;
h | m | s
----+----+----
10 | 20 | 30
(1 row)To extract the day of week and or day of year from a time stamp, you use the following statement:
SELECT date_part('dow',TIMESTAMP '2017-03-18 10:20:30') dow,
date_part('doy',TIMESTAMP '2017-03-18 10:20:30') doy;
dow | doy
-----+-----
6 | 77
(1 row)