mysql - How to calculate date using interval from table -


i have table start_date, end_date , interval. update end_date value of start_date , interval.

create table date_test (     start_date      date,     end_date        date,     date_interval   varchar(45) ); 

the values using date_interval - interval 1 week, + interval 1 month.

i like:

update date_test set end_date = date( concat( start_date, " ", date_interval)); 

but warning:

1292 truncated incorrect date value: '2012-01-01 - interval 1 week'

how can force date evaluated before updating?

jonathan leffler said :

nearly; there's crucial difference between manual page , question, though. manual discusses date_add(date_value, interval '1' day) etc, whereas question having 'string' value second parameter. fear question need function convert string interval type. there doesn't appear 'to_interval' function in mysql.

here function take date first parameter , string interval second parameter.

simply add following stored procedure database :

create procedure my_date_add(d date, varchar(50)) begin   declare sign char(1);   declare x int;    set sign = substring_index(i, ' ', 1);   set x = substring_index(substring_index(i, ' ', -2), ' ', 1);    if sign = '-'     set x = -x;   end if;    case substring_index(i, ' ', -1)     when 'day' select date_add(d, interval x day);     when 'week' select date_add(d, interval x week);     when 'month' select date_add(d, interval x month);   end case; end 

then should able update table :

update date_test set end_date = my_date_add(start_date, date_interval); 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -