I’m trying to do a request that looks like this
SELECT field1, field2, field3 = CASE
WHEN field2 = 'something' THEN 'something'
WHEN field1 IS NOT NULL and field2 IS NULL THEN 'somethingElse'
ELSE NULL
END
FROM SomeTable
WHERE field3 IS NOT NULL
This results in a syntax error. I have to rewrite the CASE in the WHERE instead of just refering to it. Is there a better way to achieve this ?
And out of curiosity, why is “WHERE field3 IS NOT NULL” refused while for example “ORDER BY field3” would pass ?
SELECT * FROM ( SELECT field1, field2, CASE WHEN field2 = 'something' THEN 'something' WHEN field1 IS NOT NULL and field2 IS NULL THEN 'somethingElse' ELSE NULL END as field3 FROM your_table ) T WHERE field3 IS NOT NULL