how to reuse a CASE in the WHERE ?

Posted by Thibault Witzig on Stack Overflow See other posts from Stack Overflow or by Thibault Witzig
Published on 2011-01-05T08:30:02Z Indexed on 2011/01/06 0:55 UTC
Read the original article Hit count: 129

Filed under:
|

Hello

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 ?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server