I have a table Sample
with data stored like below:
Id | Name |
---|---|
1 | abc, def, ghi |
2 | cbc, deb, bbb, cbc |
3 | aaa, bbb, ccc |
4 | ddd, ddd, eee |
I need output like:
Id | Name |
---|---|
1 | abc, def, ghi |
3 | aaa, bbb, ccc |
CodePudding user response:
A combination of EXISTS()
, STRING_SPLIT()
and an appropriate GROUP BY
clause is an option:
SELECT *
FROM Sample s
WHERE NOT EXISTS (
SELECT 1
FROM STRING_SPLIT(s.Name, ',')
GROUP BY RTRIM(LTRIM([value]))
HAVING COUNT(*) > 1
)
CodePudding user response:
Thanks for your help. Its working fine in Sql Server 2016 but the STRING_SPLIT function in SQL Server 2014 is not available.