Select with YEAR and MONTH together

Asked

Viewed 59 times

4

Good is possible together the year and the month, and spend a party like this 20017-06 for the select?

SELECT SUM(valor) FROM contas where YEAR(data) = '2017' and MONTH(data) = '06'

1 answer

3


Yes it is possible, use the function date_format() passing the desired format in place of the two functions. Y for the 4-digit year and m for the month with two digits.

SELECT SUM(valor) FROM contas where date_format(data, '%Y-%m') = '2017-07'
  • Perfect, that’s exactly what I needed here. Vlw brother.

  • opa, vlw. this will help a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.