wav
wav

Reputation: 1552

Select only year from MySql

I need to select only the year from the record date.

SELECT *, DATE_FORMAT('release','%Y') AS release_year FROM books

but doesnt work. The result in phpmyadmin is NULL.

Upvotes: 1

Views: 7417

Answers (3)

Rixhers Ajazi
Rixhers Ajazi

Reputation: 1313

Your problem :

DATE_FORMAT('release','%Y')

in Mysql that is a string.

Fix :

SELECT *, DATE_FORMAT(`release`,'%Y') AS release_year FROM books;

Upvotes: 1

Bill Karwin
Bill Karwin

Reputation: 562320

Don't put release in quotes. You're trying to extract the year from a literal string 'release', not the value in the column `release`.

And as @invisal states, RELEASE is a reserved word in MySQL, so you have to delimit it with back-ticks.

Upvotes: 2

invisal
invisal

Reputation: 11171

SELECT *, YEAR(`release`) AS release_year FROM books

I think release is a MySQL keyword. Try to wrap it around ``

Upvotes: 6

Related Questions