select the name of the date using sql

0

I want to make a query using SQL that allows me to see the records of all months by a parameter that I will send is the year how do I get the name of the month and then pass it to laravel code. here I leave the SQL code of the query

SELECT mes_traslado, SUM(numero_inicial) AS total_anual FROM 
formulario_precebo WHERE año_destete = '2010' GROUP BY mes_traslado;
    
asked by Juan Esteban Yarce 26.02.2018 в 14:54
source

1 answer

0

You can make combined use of DATE_FORMAT and STR_TO_DATE .

To show the names of the months in Spanish, you can use, before the consultation, lc_time_names .

For example:

SET lc_time_names = 'es_ES';

SELECT 
DATE_FORMAT(STR_TO_DATE(mes_traslado, '%m'), '%M') as mes,
SUM(numero_inicial) AS total_anual 
FROM formulario_precebo WHERE año_destete = '2010' 
GROUP BY mes_traslado;
  

IMPORTANT NOTE: Since the data is declared as a type that is not your own (in this case a VARCHAR when it should be   a DATETIME ). The maintenance and operation of the database   It is done uphill. In many cases the consultations will give errors   (for example in calculations, in columns that have a month stored    13 that does not exist, etc). It is therefore advisable that, if you have   privileges to modify the database, that you draw a plan of   normalization, which would be very simple:

     
  • Backing up the database
  •   
  • Creation of a new column of type DATETIME or type DATE .
  •   
  • Update of that new column based on the value of the current column of type VARCHAR (if not to store exact dates,   you can do a UPDATE that you create a date that is the first day   of that month.
  •   
  • Everything seems to indicate that the year would be the value of the column año_destete (by the way, I would not create column names with ñ ,   accents or special characters).
  •   
  • In the end you would have a single column instead of two, and you would not have to use several functions to get the data you're looking for.
  •   
        
    answered by 26.02.2018 / 15:15
    source