NULL is being returned from STR_TO_DATE function when using the week number format directive (%U, %u, %V or %v) on MySQL 8.0.35. The official documentation indicates that STR_TO_DATE should be the inverse of DATE_FORMAT. I expected to get the same/similar date returned back that was provided as input to DATE_FORMAT function.
Here I've prepared a sqlfiddle, or you can use the following source code to recreate issue:
SET @_NOW=NOW(6), @_FMT='%YW%U';
SELECT
@_NOW,@_FMT,
DATE_FORMAT(@_NOW,@_FMT) AS `formatted`,
STR_TO_DATE(DATE_FORMAT(@_NOW,@_FMT),@_FMT) AS `parsed`;
Is this a bug? Am I doing something wrong? Does anyone have a workaround?
The problem is that converting to a week doesn't convert to a specific day, but MySQL data validation must validate the year, month, and day.
The following works, because it maps to a specific day (the first day of the week):