Skip to content
Advertisement

Error with syntax replacing values in the query output

I work within the SQL database, but am self taught, so there are a few holes in my knowledge. I have this query that although I would think is simple, I just cant get it right.

I have two columns, one with a ‘Name’, and the other ‘Privacy’.

I am trying to to get a result where if the Privacy column has a numerical value (3) in it, all these rows will return a ‘name suppressed’ in the Name column. And all the rows with a NULL in the Privacy column are left alone.

But I don’t want to update the database, just suppress the name in the query.

I have tried, replace, case, if then and a few more with no luck. Can someone please help me.

Thanks Michele

Advertisement

Answer

You can go for simple CASE expression. I am assuming you only want to suppress if privacy = 3.

SELECT CASE WHEN Privacy = 3 THEN 'Name Suppressed' 
            ELSE Name
            END AS Name, Privacy
FROM TableName
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement