Strings from date values with the TO_CHAR() function in PostgreSQL.

Oftentimes, you may need to convert a date value or one of its parts to a string representation. Be it reporting, visualization for ad-hoc querying, or a myriad of other things; this functionality is handy at the least. Among other numeric types, the to_char function accepts date/time values with an additional parameter of the desired string character value from said type. Let’s learn together with some simple examples below…

wooden-letter-and-number-stamps
Photo by Raphael Schaller 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.

Let’s grab the most recent ‘last_update’ date column value from the ‘customer’ table:

1
2
3
4
5
dvdrental=> SELECT MAX(last_update) FROM customer;
           max          
-------------------------
 2013-05-26 14:49:45.738
(1 row)

While there are too many examples to list in one post, we will look at a few common ones you may want to explore.

You can get a four-digit year string by passing in ‘YYYY’ with the date value in the to_char() function call:

1
2
3
4
5
6
dvdrental=> SELECT to_char(MAX(last_update), 'YYYY')
dvdrental-> FROM customer;
 to_char
---------
 2013
(1 row)

Which weekday does this date fall on?

1
2
3
4
5
6
7
8
9
10
11
12
13
dvdrental=> SELECT to_char(MAX(last_update), 'DAY')
dvdrental-> FROM customer;
  to_char  
-----------
 SUNDAY  
(1 row)

dvdrental=> SELECT to_char(MAX(last_update), 'day')
dvdrental-> FROM customer;
  to_char  
-----------
 sunday  
(1 row)

Shown above, both upper and lowercase strings are available depending on which parameter you pass in.

And the month?

1
2
3
4
5
6
7
8
9
10
11
12
13
dvdrental=> SELECT to_char(MAX(last_update), 'MON')
dvdrental-> FROM customer;
 to_char
---------
 MAY
(1 row)

dvdrental=> SELECT to_char(MAX(last_update), 'mon')
dvdrental-> FROM customer;
 to_char
---------
 may
(1 row)

Perhaps you need the actual day of the year? No problem:

1
2
3
4
5
6
dvdrental=> SELECT to_char(MAX(last_update), 'DDD')
dvdrental-> FROM customer;
 to_char
---------
 146
(1 row)

By the way, what week of the year is it? Here you go:

1
2
3
4
5
6
dvdrental=> SELECT to_char(MAX(last_update), 'WW')
dvdrental-> FROM customer;  
 to_char
---------
 21
(1 row)

Visit these great resources for more ideas and examples:

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.