i have one SQL table in which many records, i want to know how many names are in it and how much time one name in it.
Table NameMst
Name
jo
You can extract the names using a recursive CTE and some string parsing. The rest is just aggregation:
with cte as (
select (case when names like '%,%'
then left(names, charindex(',', names) - 1)
else names
end) as name,
(case when names like '%,%'
then substring(names, charindex(',', names) + 1, len(names))
end) as names
from names
union all
select (case when names like '%,%'
then left(names, charindex(',', names) - 1)
else names
end) as name,
(case when names like '%,%'
then substring(names, charindex(',', names) + 1, len(names))
end)
from cte
where names is not null
)
select name, count(*)
from cte
group by name;
As you have probably figured out, storing comma delimited lists in SQL Server is a bad idea. You should have an association/junction table with one row per name (and other columns describing the list it is in).