Hossain Ahmed Saiman
Hossain Ahmed Saiman

Reputation: 69

Fetch record by Year

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?

Upvotes: 3

Views: 149

Answers (2)

SkyDrive
SkyDrive

Reputation: 1455

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

Upvotes: 5

Kneel-Before-ZOD
Kneel-Before-ZOD

Reputation: 4221

Try

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



Hope this helps.

Upvotes: 0

Related Questions