Avatar of cyber-33
Flag for United States of America asked on

Validating Python Date string before MySQL insert

I am reading some dates values from XML documents and generating MySQL insert statements. Some date values refer to month and year and are in the following format: "YYYYMo00"

For example, May 2010, would be recorded as "20100500". Although the date is expressed as "00", MySql accepts the inserts statement into the date value without any problems.

Some datevalues are incorrect (e.g. "20103400"). In this example the months cannot be 34. MySql server does not process this insert and I need to catch these improper dates prior to sending them to MySql.

How to validate strings representing dates prior to generating the MySQL Insert Statement?

I have tried using dateutil.parser.parse() function to validate the strings. This did not work because the function thought "20103400" were not valid, while MySQL Server can clearly accept these values.

I need a solution that validates date strings, while accepting the "20103400" format.

Thank you!

Avatar of undefined
Last Comment

8/22/2022 - Mon

In the question section above, the sample date value was "20100400" not "20103400".

Also note, that the dates come in the unknown format and some times may be expressed as "May 2012"

Thank you!

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Thank you.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy