开发者

how to return value from CASE expression into a ALIAS

开发者 https://www.devze.com 2023-03-30 19:06 出处:网络
I want to return a value into an alias fr开发者_Go百科om a case argument. I\'ve tried various connotations around the following

I want to return a value into an alias fr开发者_Go百科om a case argument. I've tried various connotations around the following

CASE dbo.sch_group.group_code WHEN 'ERDS' THEN '1' AS alias_name

but I get incorrect syntax warnings near the alias


You're missing an END (CASE on MSDN). This is a "simple CASE expression"

CASE dbo.sch_group.group_code
    WHEN 'ERDS' THEN '1'
END AS alias_name

If you have more conditions then it would be this

CASE dbo.sch_group.group_code
    WHEN 'ERDS' THEN '1'
    WHEN 'abcd' THEN '2'
    ELSE <something>
END AS alias_name

I also assume this is in a SELECT or such

SELECT
    col1, col2, 
    CASE dbo.sch_group.group_code
        WHEN 'ERDS' THEN '1'
        WHEN 'abcd' THEN '2'
        ELSE <something>
    END AS alias_name
FROM
    dbo.sch_group
WHERE
    ...


This is from ORACLE so I'm not sure if it'll work in SQL Server but:

(CASE WHEN dbo.sch_group.group_code = 'ERDS' THEN '1' END) AS alias_name

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号