Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add x days to a DATE-field, where x is another column [closed]

I have a table which has a DATE-column, along with a int modifier column. I'd like to somehow be able to add x days to the DATE-column, where x is the modifier's value.

Something like this:

SELECT t.dateField, DATE_ADD(t.dateField, t.dateModifierValue)
FROM fooTable t

However, that is obviously an invalid SQL-query. Here is what I am trying to achieve:

+------------+-----------+-----------------+
| DateField  |  Modifier | Expected result |
+------------+-----------+-----------------+
| 2013-05-11 |     7     |    2013-05-18   |
| 2013-01-01 |     1     |    2013-01-02   |
+------------+-----------+-----------------+

Sure, this could be done using multiple queries, letting another language build up the query — but where would the fun be in that?

like image 504
Zar Avatar asked Feb 16 '23 13:02

Zar


1 Answers

SELECT t.dateField, DATE_ADD(t.dateField, INTERVAL t.dateModifierValue DAY) FROM fooTable t;

like image 108
Chris Avatar answered Mar 27 '23 17:03

Chris