Select only year from MySql

前端 未结 3 1619
遇见更好的自我
遇见更好的自我 2020-12-21 18:57

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

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

but doesnt work.

相关标签:
3条回答
  • 2020-12-21 19:29

    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.

    0 讨论(0)
  • 2020-12-21 19:44
    SELECT *, YEAR(`release`) AS release_year FROM books
    

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

    0 讨论(0)
  • 2020-12-21 19:48

    Your problem :

    DATE_FORMAT('release','%Y')

    in Mysql that is a string.

    Fix :

    SELECT *, DATE_FORMAT(`release`,'%Y') AS release_year FROM books;
    
    0 讨论(0)
提交回复
热议问题