2

This is probably not the ideal query to retrieve rows containing specified values within a column, but the solution below effectively returns the values specified in a new column-alias but includes NULL values. How would the below query be constructed to exclude rows containing NULL values within the column alias?

SELECT student_id,
 salutation,
 CASE
 WHEN salutation IN('Dr.') THEN 'Doctor'
 END AS "NewSalutation"
FROM student.student

I welcome alternative approaches-thanks!

asked Feb 18, 2016 at 21:31

1 Answer 1

5
SELECT * FROM (
SELECT student_id,
 salutation,
 CASE
 WHEN salutation IN('Dr.') THEN 'Doctor'
 END AS NewSalutation
FROM student.student
) 
WHERE NewSalutation IS NOT NULL;

If that's your only section in the CASE statement, a logically equivalent query would be:

SELECT student_id,
 salutation,
 CASE
 WHEN salutation IN('Dr.') THEN 'Doctor'
 END AS NewSalutation
FROM student.student
WHERE salutation = 'Dr.';

... as anything other than Dr. will produce a NULL NewSalutation.

answered Feb 18, 2016 at 22:08
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.