0

I would like to compare two columns, A and B.

If Col A and Col B share the same value, I would like Col A to have a no value set. If Col A and Col B do not share the same value, I would like Col A to keep the original value it has.

Below is what I attempted. However, it returns "true" for all entries in the case column - Col A (corrected).

select *,
    case when 'Col A' = 'Col B' 
        then 'Col A' = null 
        else 'Col A' = 'Col A'
        end as "Col A (corrected)"
from my_table
1
  • ´'Col A'` isn't a column, it's a string literal - and it will never be the same as the string literal 'Col B'. Commented Jan 9, 2022 at 12:59

2 Answers 2

2
select
  case
   when col_a=col_b then null
   else col_a
 end as col_a_corrected
from my_table
Sign up to request clarification or add additional context in comments.

Comments

-1

Simply do

select *,
       NULLIF(col_a, col_b)
from my_table

The NULLIF returns null if col_a = col_b, otherwise col_a is returned.

https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-NULLIF

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.