The EXTRACT() function with examples in PostgreSQL

At times, you may only need a certain portion of a date interval or TIMESTAMP. Luckily in PostgreSQL, we have the EXTRACT() function that allows you to grab just those particular bits of data. See how with a few simple examples in this post…

manual-date-stamping-tool
Photo by Chiheb Chakchouk on Unsplash

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:
  • Xubuntu Linux 18.04.2 LTS (Bionic Beaver)
  • PostgreSQL 11.2


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


I’ll use a couple of tables from the PostgreSQL practice DVD Rental database for the example queries below.

The examples in this post are based on this arbitrary query and result set:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
SELECT
        c.first_name AS first_name,
        c.last_name AS last_name,
        p.payment_date
FROM customer AS c
INNER JOIN payment AS p
ON c.customer_id = p.customer_id
WHERE c.first_name LIKE 'Z%'
LIMIT 10;
 first_name | last_name |        payment_date        
------------+-----------+----------------------------
 Zachary    | Hite      | 2007-02-17 03:04:18.996577
 Zachary    | Hite      | 2007-02-17 06:30:46.996577
 Zachary    | Hite      | 2007-02-17 09:09:02.996577
 Zachary    | Hite      | 2007-02-17 15:01:43.996577
 Zachary    | Hite      | 2007-02-18 13:23:56.996577
 Zachary    | Hite      | 2007-02-19 15:55:51.996577
 Zachary    | Hite      | 2007-03-01 02:41:59.996577
 Zachary    | Hite      | 2007-03-02 06:48:55.996577
 Zachary    | Hite      | 2007-03-17 00:41:52.996577
 Zachary    | Hite      | 2007-03-17 11:31:39.996577
(10 rows)

Pass in the DAY parameter to get the day:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SELECT
        EXTRACT(DAY FROM p.payment_date)
FROM customer AS c
INNER JOIN payment AS p
ON c.customer_id = p.customer_id
WHERE c.first_name LIKE 'Z%'
LIMIT 10;
 date_part
-----------
        17
        17
        17
        17
        18
        19
         1
         2
        17
        17
(10 rows)

Where the returned number value is based on a 1-31 days in a month range.

If you need the day of the week, dow is there:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SELECT
        EXTRACT(dow FROM p.payment_date)
FROM customer AS c
INNER JOIN payment AS p
ON c.customer_id = p.customer_id
WHERE c.first_name LIKE 'Z%'
LIMIT 10;
 date_part
-----------
         6
         6
         6
        1 6
         0
         1
         4
         5
         6
         6
(10 rows)

Sunday starts at 0 (zero) and the days proceed through to 6 for Saturday.

What about the day of the year? No worries, doy will get it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SELECT
        EXTRACT(doy FROM p.payment_date)
FROM customer AS c
INNER JOIN payment AS p
ON c.customer_id = p.customer_id
WHERE c.first_name LIKE 'Z%'
LIMIT 10;
 date_part
-----------
        48
        48
        48
        48
        49
        50
        60
        61
        76
        76
(10 rows)

This argument returns a value between 1 and 365, representative of the days in a year.

Here’s an example with week:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SELECT
        EXTRACT(week FROM p.payment_date)
FROM customer AS c
INNER JOIN payment AS p
ON c.customer_id = p.customer_id
WHERE c.first_name LIKE 'Z%'
LIMIT 10;
 date_part
-----------
         7
         7
         7
         7
         7
         8
         9
         9
        11
        11
(10 rows)

Use month to retrieve a numerical month-of-the-year (1-12) value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SELECT
        EXTRACT(MONTH FROM p.payment_date)
FROM customer AS c
INNER JOIN payment AS p
ON c.customer_id = p.customer_id
WHERE c.first_name LIKE 'Z%'
LIMIT 10;
 date_part
-----------
         2
         2
         2
         2
         2
         2
         3
         3
         3
         3
(10 rows)

I’ve only covered but a few of the available arguments you can use with EXTRACT() so be sure and visit the official documentation for a full rundown of available options.

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

Explore the official PostgreSQL 11 On-line Documentation for more information.

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Advertisements

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.