开发者

SQL Update to flip sign of a value?

开发者 https://www.devze.com 2022-12-20 04:25 出处:网络
Someone entered a ton of numeric data into a 开发者_StackOverflow中文版table with the sign backwards.

Someone entered a ton of numeric data into a 开发者_StackOverflow中文版table with the sign backwards.

Is there a clean way to flip the sign in the numeric column with a SQL statement?


update my_table
  set amount = -amount
  where <whatever>


UPDATE [table] SET [column]=([column]*(-1))

You can add a WHERE clause as needed to limit which rows you are flipping signs on.


It should be straightforward.

update table set column = -column;


UPDATE MyTable
SET amount = -amount
WHERE amount = ABS(amount)

By including the amount = ABS(amount) clause you prevent unnecessary log activity and index maintenance. It's always a good idea to only update the rows that actually need it.

0

精彩评论

暂无评论...
验证码 换一张
取 消