Select record that has a date field using only the year in the where

1

How can I do a SELECT in the database in which the records have a field that the format is DATE Ex: (2017-05-15) and in the WHERE clause I want for just the year. I also wanted to know if it is better to use the dates in the database in varchar format or in the same date format.

    
asked by anonymous 15.05.2017 / 23:04

2 answers

1

You can use the Year function:

SELECT * from tabela WHERE YEAR(campo_data) = '2017'

Or Extract :

SELECT * from tabela WHERE EXTRACT(YEAR FROM campo_data) = '2017'

If it is better to use Date or Varchar will depend on the need, the data type infers on the resources that will be available. The advantage of using Date is that date-specific functions can be useful, like the one used above.

    
15.05.2017 / 23:10
1

You can also declare in a variable the current year, for example:

$data = date('Y'); 

Then just play on where !

    
15.05.2017 / 23:16