Update date by adding in bank

3

Good morning, guys.

I have in my database DATE fields for 'start date' and 'end date', so I can search at intervals. When I imported this data some dates were empty or non-conforming and were not recorded correctly.

I would like to know: is there any way I can select the 'end date' fields of my database and where it is empty and make it equal to the 'start date' + 30 (days) by doing it by mysql itself or will I have to use php?

Thanks!

    
asked by anonymous 14.06.2016 / 13:27

2 answers

3

You can use an UPDATE to update the final date field when it is NULL.

So:

UPDATE minha_tabela
   SET data_final = DATE_ADD(data_inicial ,INTERVAL 30 DAY)
 WHERE data_final IS NULL;

This directly in the bank, without needing an application.

About the DATE_ADD function:

link

    
14.06.2016 / 13:35
0

You can do it directly through the bank and without having to change the data in your table.

Use the function coalesce . It tries to get the leftmost value of the expression and if it is null the value is returned to the right.

SELECT DATA_INICIAL, COALESCE(DATA_FINAL, DATE_ADD(DATA_INICIAL, INTERVAL 30 DAY))
FROM TABELA
WHERE CONDICAO

Coalesce Documentation

    
14.06.2016 / 13:35