Link to home
Start Free TrialLog in
Avatar of cyber-33
cyber-33Flag 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.

QUESTION:
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 cyber-33
cyber-33
Flag of United States of America image

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.