Sum date with int

Asked

Viewed 665 times

4

I’m trying to make a sum of days, informed in a field int,with a field date using SQL. I tested it as follows, but it sums up the day, but ignores month and year. Is there any way to make that sum?

SELECT DATE_FORMAT(ocorrencias.dt_fechamento, '%d-%m-%Y')+50 as calendario from ocorrencias

2 answers

7


Try using the command DATE_ADD(ocorrencias.dt_fechamento, INTERVAL 50 DAY)

Example: SELECT DATE_FORMAT(DATE_ADD(expiry_date,INTERVAL 3 DAY), '%d %m %Y' )

  • 1

    Thanks! That was it!

3

Use the function date_add() mysql

SELECT DATE_FORMAT(date_add(ocorrencias.dt_fechamento, INTERVAL 50 day), '%d-%m-%Y') 
       as calendario FROM ocorrencias

Simple example:

SELECT  date_add(now(), INTERVAL 50 day)
  • good! I’ll write this down in the notebook. Thanks!

  • 1

    @Diego that’s right, take advantage and write down the function that subtracts dates, date_sub() :D

Browser other questions tagged

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