how to compare and update the decimal values in a table

Naresh y 146 Reputation points
2024-02-29T12:01:37.59+00:00

Hi team how to update the values from the below columns

condition is IMT_qty is less than the open_qty field i need to divide the differences between these two columns and update the IMT_qty column, if both the IMT_qty&open_qty are same will keep the same IMT_Qty value in the IMT_Qty column.

case when IMT_Qty < Open_Qty then IMT_Qty/Open_Qty else IMT_Qty end

Attached is the required screenshot for the output.

how to write the correct statement for update

User's image

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,697 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 110.4K Reputation points MVP
    2024-02-29T22:43:22.64+00:00

    Your screenshots does not match the description. From the screenshots it looks like you want

    UPDATE tbl
    SET  IMT_Qty = ITM_Qty - Open_Qty
    WHERE IMT_Qty > Open_Qty
    
    0 comments No comments

  2. LiHongMSFT-4306 26,791 Reputation points
    2024-03-01T01:48:34.3466667+00:00

    Hi @Naresh y

    Try this:

    UPDATE TableName
    SET IMT_Qty = CASE WHEN ITM_Qty = Open_Qty THEN IMT_Qty ELSE ABS(ITM_Qty - Open_Qty) END
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.