MySQL where STR_TO_DATE> = now

I want to get records from a MySQL database with a date today or later. Data is written to the database as VARCHAR (fieldname datum), so I need to use STR_TO_DATE. However, this request does not work:

SELECT * FROM Diensten WHERE STR_TO_DATE('datum', '%m-%d-%Y') >= DATE(NOW()) ORDER BY STR_TO_DATE('datum', '%m-%d-%Y') ASC 

I also tried CURDATE (), it doesn't work either.

The query works without the WHERE part. Any ideas on fixing the request?

+4
source share
1 answer

This expression STR_TO_DATE('datum', '%m-%d-%Y') returns NULL because the column name was quoted using a single quotation mark. It does not convert the value of the datum column, but the datum row is converted, so it results in a NULL value.

To fix, just remove the single quotes around the column name.

 SELECT * FROM Diensten WHERE STR_TO_DATE(datum, '%d-%m-%Y') >= CURDATE() ORDER BY STR_TO_DATE(datum, '%d-%m-%Y') ASC 
+2
source

Source: https://habr.com/ru/post/1492485/


All Articles