Share via

On case condition update the column value

Shambhu Rai 1,411 Reputation points
2023-02-22T12:49:36.6366667+00:00

Hi Expert,

Have 3 case condition and wants to update 2 field on then condition

Create table table1(col1 varchar(22),col2 varchar(22),col3 varchar(22),col4 varchar(22))

insert into table2 values ('1','2','0','0')

insert into table2 values ('3','4','0','0')

insert into table1 values ('5','4','0','0')

case when col1=1 and col2=2 then col3=X and col4= 'R1'

case when col1=3 and col2=4 then col3=Y and col4= 'R2'

case when col1=5 and col2=6 then col3=Z and col4= 'R3'

expected output

User's image

SQL Server Reporting Services
SQL Server Reporting Services

A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.

SQL Server Integration Services
SQL Server Analysis Services
SQL Server Analysis Services

A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


4 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,621 Reputation points
    2023-02-23T02:24:09.06+00:00

    Hi @Shambhu Rai

    Try this query, added the logic that the original value is maintained when the case condition is not met.

    UPDATE #table1 
    SET col3 = CASE WHEN col1=1 and col2=2 THEN 'X' 
                    WHEN col1=3 and col2=4 THEN 'Y'
                    WHEN col1=5 and col2=6 THEN 'Z' 
    				ELSE col3 END,
        col4 = CASE WHEN col1=1 and col2=2 THEN 'R1' 
                    WHEN col1=3 and col2=4 THEN 'R2'
                    WHEN col1=5 and col2=6 THEN 'R3' 
    				ELSE col4 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".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  2. Jingyang Li 5,901 Reputation points Volunteer Moderator
    2023-02-22T16:26:31.88+00:00

    Create table table1(col1 varchar(22),col2 varchar(22),col3 varchar(22),col4 varchar(22))

    insert into table1 values ('1','2','0','0')

    insert into table1 values ('3','4','0','0')

    insert into table1 values ('5','6','0','0')

    update table1

    set col3='X',col4= 'R1'

    where col1=1 and col2=2 ;

    update table1

    set col3='Y',col4= 'R2'

    where col1=3 and col2=4

    update table1

    set col3='Z',col4= 'R3'

    where col1=5 and col2=6

    select * from table1

    drop table table1;

    Was this answer helpful?

    0 comments No comments

  3. Shambhu Rai 1,411 Reputation points
    2023-02-22T15:17:22.56+00:00

    checking the solution

    Was this answer helpful?

    0 comments No comments

  4. Naomi Nosonovsky 8,906 Reputation points
    2023-02-22T13:49:23.7833333+00:00

    Try to set each column separately, e.g.

    DECLARE @t TABLE (col1 varchar(22),col2 varchar(22),col3 varchar(22),col4 varchar(22))
    insert into @t values ('1','2','0','0')
    insert into @t values ('3','4','0','0')
    insert into @t values ('5','6','0','0')
    SELECT * FROM @t
    UPDATE @t SET col3 = 
    case when col1=1 and col2=2 then 'X' ---  and col4= 'R1'
    when col1=3 and col2=4 then 'Y'-- and col4= 'R2'
    when col1=5 and col2=6 then 'Z' END,
    col4= case when col1=1 and col2=2 then 'R1' ---  and col4= 'R1'
    when col1=3 and col2=4 then 'R2'
    when col1=5 and col2=6 then 'R3' END;
    SELECT * FROM @t
    

    Was this answer helpful?

    0 comments No comments

Your answer

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