Structuring candidate date values with the STR_TO_DATE function in MySQL – with examples.

I visit Stackoverflow from time to time, curious about problems and issues plaguing developers, similar to my own. More importantly, I am drawn to the answers provided by the community to those problems. Recently, I read a question I was not 100% sure of the solution myself. Trust me, this is the case more often than not (haha). Perfect! Another opportunity to learn something new and here I am, sharing it with y’all…


overhead-shot-of-calendar-on-desk

Photo by rawpixel 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 16.04.5 LTS (Xenial Xerus)
  • MySQL 5.7.24

The questions’ focus is on handling specific date options in a query using a date value not formatted in the standard ‘YYYY-MM-DD’ that is typical in MySQL.

Several months back, I wrote the blog post, Handy MySQL Date functions with examples, in which I visited several useful date functions, one of which is the STR_TO_DATE function used extensively in this current post.

In MySQL, a date value of January 22, 2017, would be stored as 2017-01-22. For this individual case, the question concerned dates stored in an ‘MM-DD-YYYY’ format as in, 01-22-2017.

So how would you query or otherwise, manipulate a date value, not in the ‘standard’ format mentioned above? In practice, I felt the unstructured date value would first have to be converted to an acceptable one.

Admittedly, prior to writing this blog post, I was unsure of the exact structuring needed for this particular problem.
But, with a general idea and understanding of how the STR_TO_DATE() function works (see a full rundown of the function from the docs here), I dove in head first, ultimately trying several queries until I constructed one that worked.

Here is what I learned.

My first attempts below did not render the correct results:

1
2
mysql> SELECT STR_TO_DATE('01-22-2017', %d %m %Y);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%d %m %Y)' at line 1

Removing the single quotes around the date value yielded similar (incorrect) results:

1
2
mysql> SELECT STR_TO_DATE(01-22-2017, %d %m %Y);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%d %m %Y)' at line 1

I then thought to use the specifiers (see the full list of available specifiers here) for the date format I needed to convert to (‘YYYY-MM-DD’), but that did not work either:

1
2
mysql> SELECT STR_TO_DATE(01-22-2017, %Y %m %d);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%Y %m %d)' at line 1

When in doubt, what do I do?
Head to the docs!!!

After a more in-depth overview of the function, specifically the specifiers I was targeting, I devised a working query (borrowing heavily from the example provided in the documentation):

1
2
3
4
5
6
7
mysql> SELECT STR_TO_DATE('01,22,2017', '%m, %d, %Y');
+-----------------------------------------+
| STR_TO_DATE('01,22,2017', '%m, %d, %Y') |
+-----------------------------------------+
| 2017-01-22                              |
+-----------------------------------------+
1 row in set (0.00 sec)

That does the trick, yielding results in an acceptable date format.

A few points worth noting in this example: (1) The individual month, day, and year value elements are separated by commas instead of dashes(-) or forward-slashes(/); (2) The specifiers are surrounded by single quotes and also separated by commas; (3) The specifiers are listed in the same order as the parameter passed in (‘MM-DD-YYYY’) to be converted, not ordered as the final date value the function computes (‘YYYY-MM-DD’).

Below, I briefly visit the specifiers used and their meaning (For a full overview, visit the official supporting documentation through the link below):

  • %m – The month number.
  • %d – The number for the day of the month.
  • %Y – Four digit year.
Additional Reading and Resources

To see other available date and time functions in MySQL, visit the below link:

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

Explore the official MySQL 5.7 Online Manual 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, are 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.