Member-only story
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…
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:
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.
Continue reading the post here…
Originally published at https://joshuaotwell.com on June 5, 2019.