Select where the month name appears?

Asked

Viewed 96 times

1

At the moment I’m developing a app where I list on a table the month and the sum of consumption of each month in KW as shown in the print below:

inserir a descrição da imagem aqui

only that I n know how I would do to instead of listing the number of the month appears the name of the month referring to the number, at the moment I am using the following command SQL:

SELECT      ROUND(SUM(c.potencia/12))/1000  AS potencia
        ,   MONTH(c.data_criacao)
FROM        consumo c 
WHERE       c.comodo_id = :id 
GROUP BY    c.data_criacao

1 answer

2


I think for that just evoke the native method of Mysql, MONTHNAME:

SELECT      ROUND(SUM(c.potencia/12))/1000  AS potencia
        ,   MONTHNAME(c.data_criacao)       AS mes
FROM        consumo c 
WHERE       c.comodo_id = :id 
GROUP BY    c.data_criacao

If you want more work or if you really need it:

SELECT      ROUND(SUM(c.potencia/12))/1000  AS potencia
        ,   CASE MONTH(c.data_criacao)
            WHEN 1 THEN 'Janeiro'
            WHEN 2 THEN 'Fevereiro'
            WHEN 3 THEN 'Março'
            WHEN 4 THEN 'Abril'
            WHEN 5 THEN 'Maio'
            WHEN 6 THEN 'Junho'
            WHEN 7 THEN 'Julho'
            WHEN 8 THEN 'Agosto'
            WHEN 9 THEN 'Setembro'
            WHEN 10 THEN 'Outubro'
            WHEN 11 THEN 'Novembro'
            ELSE 'Dezembro'
            END                             AS mes
FROM        consumo c 
WHERE       c.comodo_id = :id 
GROUP BY    c.data_criacao
  • Thank you very much, I did not know of the existence of this function.

  • If it’s the answer to your question, don’t forget to accept @Andrépaiva

Browser other questions tagged

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