2

I seem to be missing something fundamental. I an a new refugee from an old esoteric French database system (4D) and Im new to mySQL.

Given The following DB:

CREATE  TABLE `stuff` (
  `ID` INT NOT NULL AUTO_INCREMENT ,
  `Product` VARCHAR(45) NULL ,
  `Sell_by` DATE NULL ,
  PRIMARY KEY (`ID`) );


INSERT INTO `Stuff` (`Product`, `Sell_by`) VALUES ('Milk', '2013-05-16');
INSERT INTO `Stuff` (`Product`, `Sell_by`) VALUES ('Cheese', '2013-06-15');
INSERT INTO `Stuff` (`Product`, `Sell_by`) VALUES ('Yogurt', '2013-07-02');
INSERT INTO `Stuff` (`Product`, `Sell_by`) VALUES ('Bread', '2013-08-17');
INSERT INTO `Stuff` (`Product`, `Sell_by`) VALUES ('Twinkies', '2099-04-16');

http://sqlfiddle.com/#!2/3ef48e/1

Why doesn't this return Milk Cheese and Yogurt? It returns nothing.

SELECT * FROM  Stuff Where Sell_by <= 2013-07-04;

But this returns everything?

SELECT * FROM  Stuff Where Sell_by >= 2013-07-04;

3 Answers 3

7

Your query is not actually checking for a date, but a numeric expression ( 2013 - 7 - 4 ).

Put the date expression in single quotes ('2013-07-14') and you should be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

ooh. Interesting. In line math functions. I should have left that archaic database system sooner. Thanks.
5

I think you are missing quotes; try this:

SELECT * FROM  Stuff Where Sell_by <= '2013-07-04';

Comments

3

Use quotes around date like:

SELECT * FROM  Stuff Where Sell_by <= '2013-07-04';

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.