T-SQL: How to make a positive value turn into the equivalent negative value (e.g "10.00" to "-10.00"
Ok so i have a DECIMAL
field called "Score". (e.g 10.00
)
Now, in my SP, i want to increment/decrement the value of this field in update transactions.
So i might want to do this:
SET @NewScore = @CurrentScore + @Points
Where @Points
is the value im going to increment/decrement.
Now lets say @Points = 10.00
.
In a certain scenario, i want 10.00
to become -10.00
So the statement would be translated to:
SET @NewScore = @CurrentScore + -10开发者_运维技巧.00
How can i do that?
I know its a strange question, but basically i want that statement to be dynamic, in that i dont want to have a different statement for incrementing/decrementing the value.
I just want something like this:
SET @Points = 10.00
IF @ActivityBeingPerformedIsFoo
BEGIN
-- SET @Points to be equivalent negative value, (e.g -10.00)
END
SET @NewScore = @CurrentScore + @Points
Can't you just multiply it by -1?
I always do 0 - @Points
. It was this way in some code I inherited. "A foolish consistency..."
Multiply @Points
by -1 in that certain scenario.
I thought of subtracting it with a multiple of 2, i.e. x - 2x
精彩评论