SQL Script to parse the FullName field to individual components- part 2

In this post I would like to focus on names which are in the following format:

Tendulkar, Sachin
Tendulkar ,Sachin
Tendulkar, Sachin R

and i want to select only first names which are below and in the below format where I don’t need comma’s, middlenames and trimmed firstname:

Name Count
Sachin 3

select PARSENAME( subq1.name+replicate('. ',2-(len(subq1.name)-len(replace(subq1.name,'.','')))),2) as FirstName
,count(1) as Cnt from (
select replace(replace(REPLACE(name,', ','.'),',','.'),' ','.') name from <names table> (nolock)
       <inner join based on your requirement>
       where <conditions based on your requirement>
) subq1
group by PARSENAME( subq1.name+replicate('. ',2-(len(subq1.name)-len(replace(subq1.name,'.','')))),2) order by Cnt desc

Hope this helps!

This entry was posted in SQL Server DBA Stuff and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s