1

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.

3 Answers 3

2

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');
Sign up to request clarification or add additional context in comments.

Comments

1

Use quotes for date.

you can write simply as follow.

SELECT * FROMsalesWHERE sale_date='2014-10-01';

Comments

1

You can use either

SELECT * FROM `sales` WHERE sale_date between "2014-10-01 00:00:00" and "2014-10-01 23:59:59";

OR

SELECT * FROM `sales` WHERE date(sale_date) = "2014-10-01";

If records are very large, then 2nd query will take time to respond than first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.