我的表格数据如下:
Description Name
ABC AB
ABCD AB, BC, CD
ABCDF AB, BC
现在我需要输出如下:
Description Name
ABC AB
ABCD AB, BC and CD
ABCDF AB and BC
如何在SQL中获得所需的输出?请帮帮我。
答案 0 :(得分:1)
将其作为
declare @str nvarchar(200)
set @str = 'Ali, ahmed, riaz, zoya'
select SUBSTRING(@str, 0, (len(@str) - charindex(',', reverse(@str)))) +
Replace(SUBSTRING(@str, (len(@str) - charindex(',', reverse(@str))), len(@str)),
', ', ' and ')
答案 1 :(得分:1)
请尝试:
select
Description,
ISNULL(
REVERSE(STUFF(REVERSE(Name), CHARINDEX(',', REVERSE(Name), 0),1,'dna ')),
Name) Name
From YourTable