10

We have a column that is a simple integer. We want to add to each row the value 10. How do we do it in sql for the MySQL database?

Actually we have another column that needs to do the same thing, and it is a date. We need to add a month to the date. How to do that?

4 Answers 4

15

Integers:

UPDATE table_name SET int_column_value = int_column_value + 10;
UPDATE table_name SET int_column_value = 10 WHERE int_column_value IS NULL;

Dates:

UPDATE table_name SET date_column_value = DATEADD(date_column_value, INTERVAL 1 MONTH);

More info: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_adddate

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

Comments

8
 UPDATE table_name SET column_value = column_value + 10;

3 Comments

what about for a date? Adding a month?
That depends on what kind of column you're storing. Is it an integer storing a timestamp, is it a datetime, is it a date? Do you want to add 10 seconds to the count, 10 hours, 10 days, what?
if column value is null it will not add 10 to it
2
update table_name set column_name=column_name+10 where column_name is not null;

3 Comments

What will happen if the field is already null? Will the value become 10, or will it throw an error?
if column value is null it will not add 10 to it
Sorry, what I meant to say is, if you were to omit the WHERE column_name IS NOT NULL portion of your query, what would happen. I understand that your query would prevent this occurrence completely.
2

Should be something simple like this:

UPDATE some_table SET int_field = int_field + 10

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.