SQL Update to flip sign of a value?
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.
精彩评论