Dynamic Columns and WHERE clause

  • Hello... Been awhile...

    If I have this - all works fine:

    Select

    'ANormalColumn',

    CASE

    WHEN 1 = 1 THEN 'T'

    ELSE 'F'

    END AS ADynamicColumnName

    Then if I add this - failure with "Invalid Column Name":

    WHERE

    ADynamicColumnName != 'F'

    What's the deal here?

  • You will have to either:

    a) include the case expression's switch field in the where clause, or

    b) put the whole select in a derived table then build a where expression on the derived table's alias:

    SELECT * FROM

    (

    Select

    'ANormalColumn',

    CASE

    WHEN 1 = 1 THEN 'T'

    ELSE 'F'

    END AS ADynamicColumnName

    ) as dt

    where dt.ADynamicColumnName = 'F'

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply