Search code examples
symfonydql

DQL - extract year from datetime format


How do I extract distinct years from datetime format (00-00-0000 00:00:00)? I'm using Symfony 2 and DQL.

In SQL it would be:

SELECT DISTINCT extract(year FROM created_at) 
FROM news 
ORDER BY created_at DESC

edit:

solution with SUBSTRING:

group by year (datetime format Y-m-d H:i:s):

SELECT p, SUBSTRING(p.created_at, 1, 4) as year
FROM news p
GROUP BY year

Solution

  • Also you can use Native SQL and custom hydration.