Thursday, 4 February 2010

Charindex to get Specific Domain Name from Email Address

Objective: Get the specific domain name from emailaddress column in table and group it. Also to know the distinct domain name:
Below is the SQL Query to extract the domain name from emailaddress column.
select substring(emailaddress,charindex('@',emailaddress)+1 ,len(emailaddress)) as [Emaildomain], count(*) as Counting from Tablename(nolock)
where datediff(mm,date,getdate())=1
group by substring(emailaddress,charindex('@',emailaddress)+1 ,len(emailaddress))
order by counting desc
go
select distinct(substring(emailaddress,charindex('@',emailaddress)+1 ,len(emailaddress))) as [Emaildomain] from Tablename(nolock) where datediff(mm,date,getdate())=1