0

I am trying to perform the following query:

SELECT * FROM table_name_here WHERE Date LIKE %Jan % 2014%

Now, the table name is different and hidden here, but it just won't go through. It says there is an error in my syntax around %Jan % 2014%

I can get this to work, so I know the connection works: SELECT * FROM table_name_here So the problem lies with the WHERE and LIKE part.

I also tried to perform this on my hosting sites DB management tool:

SELECT * 
FROM  `table_name` 
WHERE  `Date` LIKE  '%Jan % 2014%'

and that one works

4 Answers 4

2

You have two syntax errors, firstly the word Date is a keyword, so needs to be wrapped and you need quotes around your string, like so:

SELECT * FROM table_name_here WHERE `Date` LIKE "%Jan % 2014%"
1

Assuming that date is begin stored as a date/datetime column, don't use like on it. The like implicitly converts it to a string, using some local format.

Instead, be explicit:

where month(`date`) = 1 and year(`date`) = 2014

or

where date_format(`date`, '%Y-%m') = '2014-01'

As for your original question, you discovered that quotes are important around string constants. I would recommend using single quotes (as opposed to double quotes), because single quotes are the ANSI standard string delimiter.

0

You need quotes around your LIKE match:

SELECT * FROM table_name_here WHERE Date LIKE "%Jan % 2014%"
0

If you are using the DATE datatype, you can use this

SELECT * FROM table_name_here WHERE 'Date' LIKE '2014-01-%'

this is how mysql store the DATE value: 'YYYY-MM-DD' eg. '2014-01-07.

MySQL Manual

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.