Select statement with condition

chuck DM 101 Reputation points
2023-11-10T01:27:50.45+00:00

I have the following tables:-

User's image

Now I get the following result:-

Basically, I want if Code in ('A') then Code_Respon and if Code in ('B') then Code_Respon. How to write this query?

Thanks in Advance

User's image

SQL Server | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-11-10T01:54:29.3033333+00:00

    Hi @chuck DM

    Your description confuses me. I am not sure if this is what you want.

    create table test(Code char(1),Code_Respon varchar(20),Code_share varchar(10));
    insert into test values
    ('A','R101','S11'),
    ('B','R1001','S12'),
    ('A','R201','S13'),
    ('C','R101','S14');
    
    select Code,case when Code in ('A','B') then Code_Respon end Code_Respon,Code_share from test;
    

    Output:

    User's image

    Best regards,

    Percy Tang

    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.