-4

我有一个字符串,其中的代码用逗号分隔,

例子:

@inputText = "1,2,3,4,5"

我需要转换为:

@outputText = "apple, peach, orange, banana, onion"

我有代码表:

id name
-- ----
1  apple
2  peach
3  orange
4  banana
5  onion

我正在使用 sql server 2008

我需要执行此操作的存储过程代码,谢谢。

4

1 回答 1

4

您可以使用FOR XML PATHwithLIKE替换逗号分隔列表中的值:

declare @text varchar(50) = '1,2,3,4,5'

;with cte as 
(
    select @text list
) 
select 
(
    select ', '+c.name
    from codes c
    where ','+t1.list+',' like '%,'+cast(c.ID as varchar(10))+',%'
    for xml path(''), type
).value('substring(text()[1], 3)', 'varchar(max)') as NewList
from cte t1

这是基于@Mikael Eriksson的回答

于 2013-03-05T10:54:20.450 回答