SQL SELECT CASE 测试和连接多个字符串

问题描述 投票:0回答:1

这能做到吗?尝试在 SQL CASE 语句中连接 (||) 可能的多个结果。

示例:l.Reason 可以是:'R01|R04' 那么 Reason 应该是:“Result1 Result4”

SELECT case 
    when CHARINDEX('R01',l.Reason) > 0 then 'Result1'
    when CHARINDEX('R02',l.Reason) > 0 then || ' Result2'
    when CHARINDEX('R03',l.Reason) > 0 then || ' Result3'
    when CHARINDEX('R04',l.Reason) > 0 then || ' Result4'
    end Reason
FROM totals y

(SSMS 说 || 语法不正确)

sql-server case
1个回答
0
投票

我只是猜你在寻找这个解决方案:

这将创建一个新文本,将 R01 替换为“Result1”,将 R02 替换为“Result02?等等。

SELECT
    REPLACE(
        REPLACE(
            REPLACE(
                REPLACE(l.Reason, 'R01', 'Result1'),
            'R02', ' Result2'),
        'R03', ' Result3'),
    'R04', ' Result4') AS Reason
FROM totals l --not sure who you use y as an alias
© www.soinside.com 2019 - 2024. All rights reserved.