Convert Field Data Date to Month Mysql

5

In a DB table I have a Data Field (0000-00-00) and I need to do the following: Create a field next to 'Month' that takes the date Ex: 2014-08-27 and pass the 'August' value to the month field. And so on with all the records in the Data field. How to do this?

    
asked by anonymous 27.08.2014 / 14:12

4 answers

5

What would be necessary to create the field, the column:

ALTER TABLE tabela ADD COLUMN mes VARCHAR(255);

And after that run the update:

SET lc_time_names = 'pt_BR';
UPDATE tabela SET mes = (SELECT MONTHNAME(data)) WHERE mes IS NULL;

This will generate the month in full in Portuguese.

    
27.08.2014 / 15:36
4

You can create a query that manages the updates like this

set lc_time_names = 'pt_BR';

SELECT concat('UPDATE tabela SET mes_extenso = \'', monthname(data), '\' WHERE id =', id)
FROM datas

lc_time_names , define the locale that is responsible for translating the name the month in Portuguese and monthname () returns the month name in full from a date:

select monthname(now())

output:

Agosto ou august

Example

    
27.08.2014 / 15:16
1

Change your table in the database with alter table

ALTER TABLE 'tabela' 
ADD COLUMN 'mes' VARCHAR(12) NULL;

Then, from a update in this table, with your date:

UPDATE 'tabela' SET 'mes' = date_format('data', '%M')

This will populate the mes column with the month in English.

A second update may be needed to translate to Portuguese, something like this:

UPDATE 'tabela' SET 'mes' =
CASE mes
  WHEN 'January'   THEN 'Janeiro'
  WHEN 'February'  THEN 'Fevereiro'
  WHEN 'March'     THEN 'Março'
  WHEN 'April'     THEN 'Abril'
  WHEN 'May'       THEN 'Maio'
  WHEN 'June'      THEN 'Junho'
  WHEN 'July'      THEN 'Julho'
  WHEN 'August'    THEN 'Agosto'
  WHEN 'September' THEN 'Setembro'
  WHEN 'October'   THEN 'Outubro'
  WHEN 'November'  THEN 'Novembro'
  WHEN 'December'  THEN 'Dezembro'
ELSE 'mes';

Alternatively, before update , you can use lc_time_names so you do not need to translate the months name:

SET lc_time_names = 'pt_BR';

UPDATE 'tabela' SET 'mes' = date_format('data', '%M')
    
27.08.2014 / 15:12
1

Recording to base in some field:

update tabela1 set dataextenso = (CASE month(data) 
         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'
         when 12 then 'Dezembro'
         END) 

Without writing to the base:

Select id, data, mes
FROM (
SELECT
      day(data) AS dia,
      year(data) AS ano,    
      (CASE month(data) 
         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'
         when 12 then 'Dezembro'
         END) AS mes,
         id, 
         data 
FROM tabela1
WHERE not (data is null)
) as tabela1

Another without writing to the base:

Select id, data, mes
FROM (
SELECT
      day(data) AS dia,
      year(data) AS ano,    
      (CASE monthname(data) 
         when 'January' then 'Janeiro'
         when 'February' then 'Fevereiro'
         when 'March' then 'Março'
         when 'April' then 'Abril'
         when 'May' then 'Maio'
         when 'June' then 'Junho'
         when 'July' then 'Julho'
         when 'August' then 'Agosto'
         when 'September' then 'Setembro'
         when 'October' then 'Outubro'
         when 'November' then 'Novembro'
         when 'December' then 'Dezembro'
         END) AS mes,
         id, 
         data 
FROM tabela1
WHERE not (data is null)
) as tabela1
    
27.08.2014 / 15:22