Very interesting technique madhivanan. Here is an XML version.
declare @t table(email varchar(100))
insert into @t
select 'test@yahoo.com' union all
select 'test@msn.com' union all
select 'test@gmail.com' union all
select 'test1@yahoo.com' union all
select 'test@sify.com' union all
select 'test2@yahoo.com' union all
select 'test3@msn.com' union all
select '' union all
select null union all
select 'test1@gmail.com'
;WITH cte AS (
SELECT CAST(
'<i>' + REPLACE(email, '@', '</i><i>') + '</i>'
AS XML).value('/i[2]', 'varchar(50)') AS Domain
FROM @t
)
SELECT
Domain,
COUNT(*) AS Cnt
FROM cte
WHERE Domain IS NOT NULL
GROUP BY Domain
ORDER BY COUNT(*) DESC
/*
Domain Cnt
-------------------------------------------------- -----------
yahoo.com 3
gmail.com 2
msn.com 2
sify.com 1
*/
commented on Jun 20 2011 8:03AM