SQL: Alias Column Name for Use in CASE Statement

前端 未结 11 1781
悲哀的现实
悲哀的现实 2020-12-04 21:38

Is it possible to alias a column name and then use that in a CASE statement? For example,

SELECT col1 as a, CASE WHEN a = \'test\' THEN \'yes\' END as value          


        
11条回答
  •  再見小時候
    2020-12-04 22:04

    Nor in MsSql

    SELECT col1 AS o, e = CASE WHEN o < GETDATE() THEN o ELSE GETDATE() END 
    FROM Table1
    

    Returns:

    Msg 207, Level 16, State 3, Line 1
    Invalid column name 'o'.
    Msg 207, Level 16, State 3, Line 1
    Invalid column name 'o'.
    

    However if I change to CASE WHEN col1... THEN col1 it works

提交回复
热议问题