Corrected bug in the to_date in SQL query