Вы должны создать функцию, аналогичную приведенной ниже.
CREATE FUNCTION [dbo].[split](
@delimited NVARCHAR(MAX),
@delimiter NVARCHAR(100)
) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
AS
BEGIN
DECLARE @xml XML
SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'
INSERT INTO @t(val)
SELECT r.value('.','varchar(MAX)') as item
FROM @xml.nodes('/t') as records(r)
RETURN
END
, затем, например,
create table #Fruits (
id int identity(1,1),
string nvarchar(100)
)
insert into @Fruits (string) values ('Apple,Mango'), ('Orange'), ('Apple')
select val,val+'(' +cast(count(val) as varchar(10))+')'val
from @Fruits
cross apply dbo.split(string,',')
group by val