Does MySQL provide any function that checks for the correct date? The DATE function returns NULL when providing an invalid date 2013-02-30, for example. However, I also use STR_TO_DATE at the same time, which mysteriously stops DATE from working properly.
SELECT DATE('2013-02-30'); NULL SELECT STR_TO_DATE('2013-02-30', '%Y-%m-%d'); NOT NULL SELECT DATE('2013-02-40'); NULL SELECT STR_TO_DATE('2013-02-40', '%Y-%m-%d'); NULL SELECT DATE(STR_TO_DATE('2013-02-30', '%Y-%m-%d')); NOT NULL
Why is the STR_TO_DATE function halt DATE and is there some workaround to check if the date is valid when using STR_TO_DATE (which I must use)?
I came across an answer in the meantime: apparently the DATE function skips several validation checks when the data type is already of the date type ( STR_TO_DATE converts the data types to date). Therefore, converting the date to a string after parsing it in the correct format using STR_TO_DATE does the trick:
@valid_date = NOT ISNULL(DATE(CONVERT(STR_TO_DATE('2013-02-29', '%Y-%m-%d'), CHAR))) .
source share