SQL: Alias Column Name for Use in CASE Statement

前端 未结 11 1778
悲哀的现实
悲哀的现实 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 21:56

    @OMG Ponies - One of my reasons of not using the following code

    SELECT t.col1 as a, 
         CASE WHEN t.col1 = 'test' THEN 'yes' END as value 
    FROM TABLE t;
    

    can be that the t.col1 is not an actual column in the table. For example, it can be a value from a XML column like

    Select XMLColumnName.value('(XMLPathOfTag)[1]', 'varchar(max)') 
    as XMLTagAlias from Table
    

提交回复
热议问题