Comparison of two dates using Mysql varchar type

Asked

Viewed 416 times

0

I have a question regarding the preparation of an SQL query in which I pass a registration date and check if it is bigger than the current date, but I have a problem the registration date is varchar and follows the format "dd/mm/yyyy", follows below the select:

SELECT * FROM Evento 
WHERE data_evento > curdate()
ORDER BY idEvento ASC LIMIT 0,3;
  • The date format is different and the type should be date/datetime to avoid other problems. The ideal is to arrange this in a new column if not possible the gain break is Date (varchar) to date.

  • @rray thanks for the tip, I even know it is not correct but unfortunately I can not change, certainly changing the format would work perfectly, but will not be possible?

2 answers

1


Convert the field to date on its own SELECT using the function STR_TO_DATE:

SELECT * FROM Evento 
WHERE STR_TO_DATE(data_evento, "%d/%m/%Y") > curdate()
ORDER BY idEvento ASC LIMIT 0,3;
  • Thanks Roberto, it worked perfectly!

0

Syntax for CAST: CAST ( Expression AS data_type [ ( length ) ]) Syntax for CONVERT: CONVERT ( data_type [ ( length ) ] , Expression [ , style ] )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.