Want to convert a negative number to a positive

I have the following query that I used in the procedure and I want to convert one number to positive, since now it is negative.

 UPDATE SHIPMENT
        SET TOTAL_SHIP_UNIT_COUNT = (
          CASE
            WHEN V_SHIP_UNIT_COUNT > v_diff_cost
            THEN V_SHIP_UNIT_COUNT - v_diff_cost
            ELSE v_diff_cost       - V_SHIP_UNIT_COUNT
          END)
        WHERE SHIPMENT_GID = v_shipment_id;
        COMMIT;

in this query, the value of v_diff_cost is negative, so when the action ( V_SHIP_UNIT_COUNT - v_diff_cost)is performed, it adds both values, therefore, if I convert the value v_diff_costto positive, then subtraction will give me the correct result.

Suppose that the value V_SHIP_UNIT_COUNTis 33, and v_diff_cost value-10, then in this case it should perform the action as 33-10 = 23, but it is executed as 33-(-10)= 43, and this should not be.

kindly help me. Thanks

+4
source share
3 answers

ABS(),

MSDN: , () .

UPDATE SHIPMENT
    SET TOTAL_SHIP_UNIT_COUNT = (
      CASE
        WHEN V_SHIP_UNIT_COUNT > v_diff_cost
        THEN V_SHIP_UNIT_COUNT - ABS(v_diff_cost)
        ELSE ABS(v_diff_cost)       - V_SHIP_UNIT_COUNT
      END)
    WHERE SHIPMENT_GID = v_shipment_id;
    COMMIT;
+4

:

UPDATE SHIPMENT
        SET TOTAL_SHIP_UNIT_COUNT = (
          CASE
            WHEN V_SHIP_UNIT_COUNT > v_diff_cost
            THEN V_SHIP_UNIT_COUNT - abs(v_diff_cost)
            ELSE v_diff_cost       - abs(V_SHIP_UNIT_COUNT)
          END)
        WHERE SHIPMENT_GID = v_shipment_id;
        COMMIT;
0

Try the following:

UPDATE SHIPMENT
SET TOTAL_SHIP_UNIT_COUNT = ABS(V_SHIP_UNIT_COUNT - ABS(v_diff_cost)) 
WHERE SHIPMENT_GID = v_shipment_id;
0
source

All Articles