Bibin
Bibin

Reputation: 274

Dispalying Column value depend on the particular condition otherwise displaying emply column value

I have the following table

Id  col1 status Action

1    c    R     close

2    c    S     close

3    e    R     close

4    1    N     close

5    2    N    close

6    4    N     close

I want to display all the record with the following condition

1) Action column value will only when the status is N. Otherwise it will be empty

Required output

Id  col1 status Action

1    c    R   

2    c    S     

3    e    R     

4    1    N     close

5    2    N     close

6    4    N     close

I am using SQL SERVER 2008 R2

Any reply will be appreciated.

Upvotes: 0

Views: 20

Answers (2)

Krishnraj Rana
Krishnraj Rana

Reputation: 6656

Try this

SELECT Id
    ,col1
    ,STATUS
    ,Action = CASE 
        WHEN [STATUS] = 'N'
            THEN [Action]
        ELSE ''
        END
FROM yourTable

Upvotes: 1

Caleth
Caleth

Reputation: 63019

You want a query using CASE

E.g.

SELECT Id, col1, status, CASE WHEN status = 'N' THEN Action ELSE '' END AS Action
FROM table

Upvotes: 0

Related Questions