How can I execute the following query?

2

I want to be able to get the name of the months by extracting the month from a field that is called FECHA_RECIBE but in the MySql console it tells me that there is an error near FROM . the rest of the query works correctly. The query is as follows:

SELECT SUM(VALOR) as costos CASE
WHEN MONTH(FECHA_RECIBE) = 1 THEN 'Enero'
WHEN MONTH(FECHA_RECIBE) = 2 THEN 'Febrero'
WHEN MONTH(FECHA_RECIBE) = 3 THEN 'Marzo'
WHEN MONTH(FECHA_RECIBE) = 4 THEN 'Abril'
WHEN MONTH(FECHA_RECIBE) = 5 THEN 'Mayo'
WHEN MONTH(FECHA_RECIBE) = 6 THEN 'Junio'
WHEN MONTH(FECHA_RECIBE) = 7 THEN 'Julio'
WHEN MONTH(FECHA_RECIBE) = 8 THEN 'Agosto'
WHEN MONTH(FECHA_RECIBE) = 9 THEN 'Septiembre'
WHEN MONTH(FECHA_RECIBE) = 10 THEN 'Octubre'
WHEN MONTH(FECHA_RECIBE) = 11 THEN 'Noviembre'
WHEN MONTH(FECHA_RECIBE) = 12 THEN 'Diciembre'
FROM huawei_combustible WHERE YEAR(FECHA_RECIBE) = '2017' AND 
MONTH(FECHA_RECIBE) = '12' AND DAY(FECHA_RECIBE) = '20' AND CIUDAD = 'CALI'
    
asked by Anderviver 05.01.2018 в 22:07
source

1 answer

4

You can simplify your query significantly by using DATE_FORMAT , indicating that you give the name of the month with %M .

For example:

SELECT 
    SUM(VALOR) as costos,
    DATE_FORMAT(FECHA_RECIBE, '%M') as mes
FROM huawei_combustible 
    WHERE YEAR(FECHA_RECIBE) = '2017' 
    AND MONTH(FECHA_RECIBE) = '12' 
    AND DAY(FECHA_RECIBE) = '20' 
    AND CIUDAD = 'CALI';

If you get the months in English, you can change it with SET lc_time_names to establish (temporarily or permanently) the exits of dates in Spanish.

    
answered by 05.01.2018 / 22:40
source