Emelia Johansson
Emelia Johansson

Reputation: 11

If one column is null, replace another column with null as well

I am joining two tables, case and user. My problem is that I want to sensor the user's name and location if he/she is working at location B-D.

I want this

Default

To look like this

Censored

This is my code so far, but the name is not censored:

SELECT 
c.CaseID, 
c.Category, 
u.User, 
CASE 
    WHEN u.Location != "A" THEN null
    ELSE u.Location
END as Location

FROM cases c

JOIN c.userID on u.userID

Upvotes: 0

Views: 276

Answers (1)

M.Ali
M.Ali

Reputation: 69524

Use the same case statement for user column too.

SELECT 
c.CaseID, 
c.Category, 
CASE 
    WHEN u.Location != "A" THEN null
    ELSE u.User
END as User,
CASE 
    WHEN u.Location != "A" THEN null
    ELSE u.Location
END as Location
FROM cases c...... <Your remaining query>

Upvotes: 5

Related Questions