Tuesday, 28 August 2018

Mysql query does not return the correct value when the date field in the condition where

In my sales table the sale_date field is in date format

When I run the query,
SELECT * FROM `sales` WHERE sale_date=2014-10-01;

Return no result. Actually the sale_date field contains the value 2014-10-01.

without the quotes, this is interpreted as three integers with the subtraction operator (-) between them: 2014 - 10 - 01 = 2003. You need to express this value as a date, e.g., with the str_to_date function:
SELECT *
FROM   `sales`
WHERE  sale_date = STR_TO_DATE('2014-10-01', '%m-%d-%Y');

0 comments:

Post a Comment