3

I have a form with a text field, where the user will provide the year only and MySQL data should return all the data for the given year. But in MySQL database, the date is given in date(yyyy-mm-dd) format.

Can anyone suggest the SQL query which will fetch all the data for that particular year?

2 Answers 2

5

try this one, use the MySQL YEAR function.

SELECT *
FROM tableName
WHERE YEAR(dateCOlumn) = 2012

so in the given example, the server will select all records where year is equal to 2012.

UPDATE 1

SQLFiddle Demo

YEAR

0

Try

 
  select * from tablename where year(columname) = 'whatever the year is';
 



Hope this helps.

2
  • 2
    What's the difference between your answer and Derek's?
    – undone
    Dec 8, 2012 at 6:11
  • Difference of 2 mins, in which case we were both typing the same answer, except that mine came 2 minutes later :) Dec 8, 2012 at 6:20

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.