Home > Back-end >  How to filter row in sql if column with comma seprated that contains with duplicate values
How to filter row in sql if column with comma seprated that contains with duplicate values

Time:02-19

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.

  • Related