0

is it possible to use arguments in DATE_ADD as values from some field in table.

SELECT DATE_ADD(some_date, INTERVAL some_number WEEK) as new_date FROM table1

Is it possible to replace "WEEK" by some value from my table. Let's say i have a column with values DAY, WEEK, MONTH etc.?

1 Answer 1

1

No. You only can have a dynamic value for the expression but not for the unit keywords on intervals. Unit keywords can be SECOND, DAY_MICROSECOND, etc..
Concatenation of these unit names will result a string expression and can't be evaluated as an interval value. Hence you can't use place holders (prepared statement) to achieve this.

Alternatively you can generate the query by concatenating partial strings, in your programming language.

String part1 = "SELECT DATE_ADD(some_date, INTERVAL some_number ";  
String part2 = "WEEK"; // say as default value  

if( my_condition_1_is_satisfied ) then part2 = "DAY"
else if( my_condition_2_is_satisfied ) then part2 = "MONTH"
else ...

// then concatenate part1 and part 2
String sql = part1 + part2;
// now your query string is ready to use  

Refer to: DATE_ADD(date,INTERVAL expr unit)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.