2

Please take a look at this and let me know the possible solution?

Data to be shown from the table:

select a,b,d,e from table xyz.
  • when c is null show d with value

or

  • when c is not null show e with value

Data required:

Data looks like this

a b  c          d e 
1 2  null         2
1 2  not null   2  

From the above data, if c is null, display d = b else e = b.

How to write a proper SQL query for the above conditions, as I tried case it is not working.

Thanks in advance.

1
  • You want to try again. Specs like this from customers are excusable... Commented Jul 30, 2013 at 21:40

2 Answers 2

2
SELECT CASE WHEN c IS NULL THEN d ELSE e END

The bit about display d = b else e = b leads me to believe you may also be trying to compare NULL and NOT NULL values.

It's important to understand that SQL NULL means "Unknown" and therefore a comparison cannot take place between a known value and an unknown value.

In this case I suggest the use of Coalesce to change the value when it is NULL to something comparable that will not affect your logic.

Coalesce(d, 0) = b
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT CASE WHEN c IS NULL THEN d ELSE e END

1 Comment

You're missing a keyword. The correct syntax here is: SELECT CASE WHEN c IS NULL THEN d ELSE e END

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.