Working With DATE, TIME Functions and Operators PDF
Working With DATE, TIME Functions and Operators PDF
arithmetic operators
F U N C T I O N S F O R M A N I P U L AT I N G D ATA I N P O S TG R E S Q L
Brian Piccolo
Sr. Director, Digital Strategy
Topics
Overview of basic arithmetic operators
+---------+
| integer |
|---------|
| 1 |
+---------+
+------------+
| date |
|------------|
| 2005-09-14 |
+------------+
+----------------+
| interval |
|----------------|
| 1 day 12:00:00 |
+----------------+
+----------------+
| interval |
|----------------|
| 1 day 12:00:00 |
+----------------+
+-----------------------------------+
| age |
|-----------------------------------|
| 13 years 11 mons 12 days 01:06:30 |
| 13 years 11 mons 12 days 01:05:27 |
| 13 years 11 mons 12 days 00:56:21 |
+-----------------------------------+
+---------------------+
| expected_return |
|---------------------|
| 2005-05-27 22:53:30 |
+---------------------+
+----------------------------+
| timestamp without timezone |
|----------------------------|
| 2019-05-22 00:00:00 |
+----------------------------+
Brian Piccolo
Sr. Director, Digital Strategy
Retrieving the current timestamp
SELECT NOW();
+-------------------------------+
| now() |
|-------------------------------|
| 2019-04-19 02:51:18.448641+00 |
+-------------------------------+
SELECT NOW()::timestamp;
+----------------------------+
| now() |
|----------------------------|
| 2019-04-19 02:51:18.448641 |
+----------------------------+
CAST() function
SELECT CAST(NOW() as timestamp);
cast allows you to convert one data type to another such that cols stored in your databse can be retrieved and output as a different type
+-------------------------------+
| current_timestamp |
|-------------------------------|
| 2019-04-19 02:51:18.448641+00 |
+-------------------------------+
+-------------------------------+
| current_timestamp |
|-------------------------------|
| 2019-04-19 02:51:18.44+00 |
+-------------------------------+
+--------------+
| current_date |
|--------------|
| 2019-04-19 |
+--------------+
+------------------------+
| current_time |
|------------------------|
| 04:06:30.929845+00:00 |
+------------------------+
Brian Piccolo
Sr. Director, Digital Strategy
Extracting and transforming date and time data
Exploring the EXTRACT() , DATE_PART() and DATE_TRUNC() functions
2005-05-13 08:53:53
2005 or 5 or 2 or Friday
2005-05-13 00:00:00
+---------+
| quarter |
|---------|
| 1 |
+---------+
Now suppose you want to produce a predictive model that will help forecast DVD rental activity by day of the week. You could use the EXTRACT() function with the dow field identifier in our query to create a new field called
dayofweek
+--------------------------------------------------------------------------------+
| payment_id | customer_id | staff_id | rental_id | amount | payment_date |
|------------|-------------|----------|-----------|--------|---------------------|
| 1 | 1 | 1 | 76 | 2.99 | 2005-05-25 11:30:37 |
| 2 | 1 | 1 | 573 | 0.99 | 2005-05-28 10:35:23 |
| 3 | 1 | 1 | 1185 | 5.99 | 2005-06-15 0:54:12 |
+--------------------------------------------------------------------------------+
SELECT +---------------------------------+
EXTRACT(quarter FROM payment_date) AS quarter, | quarter | year | total_payments |
EXTRACT(year FROM payment_date) AS year, |---------|------|----------------|
SUM(amount) AS total_payments | 2 | 2005 | 14456.31 |
FROM | 3 | 2005 | 52446.02 |
payment | 1 | 2006 | 514.18 |
GROUP BY 1, 2; +---------------------------------+
SELECT
c.first_name || ' ' || c.last_name AS customer_name,
f.title,
r.rental_date,
-- Extract the day of week date part from the rental_date
EXTRACT(dow FROM r.rental_date) AS dayofweek,
AGE(r.return_date, r.rental_date) AS rental_days,
Let's practice!
-- Use DATE_TRUNC to get days from the AGE function
CASE WHEN date_trunc('day', age(r.return_date, r.rental_date)) >
-- Calculate number of d
f.rental_duration * interval '1' day
THEN TRUE
ELSE FALSE END AS past_due
FROM
film AS f
INNER JOIN inventory AS i F U N C T I O N S F O R M A N I P U L AT I N G D ATA I N P O S TG R E S Q L
ON f.film_id = i.film_id
INNER JOIN rental AS r
ON i.inventory_id = r.inventory_id
INNER JOIN customer AS c
ON c.customer_id = r.customer_id
WHERE
-- Use an INTERVAL for the upper bound of the rental_date
r.rental_date BETWEEN CAST('2005-05-01' AS DATE)
AND CAST('2005-05-01' AS DATE) + INTERVAL '90 day';