0

In the following table, I have a column called ShortDesc and one called LongDesc. If the ShortDesc is not null, I want to return this value. If the ShortDesc column in a row is null, I want to return the value of the LongDesc. If both the ShortDesc and LongDesc are not null, I only want to return the ShortDesc (the LongDesc needs to be returned as null).

Table Events

ID  ShortDesc   LongDesc
0     abc        null
1     null       def
2     ghi        jkl

Result:

ID  ShortDesc   LongDesc
0     abc        null
1     null       def
2     ghi        null

I'm at a loss how to create the SQL for this.

1 Answer 1

2

If you want to show both shortDesc and longDesc:

SELECT
   shortDesc,
   CASE WHEN shortDesc IS NOT NULL THEN NULL ELSE longDesc END AS longDesc
FROM yourTable;

If you just want to show a single desc:

SELECT COALESCE(shortDesc, longDesc) AS desc
FROM yourTable;
Sign up to request clarification or add additional context in comments.

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.