Solved

Splitting contact names into component Title/Forename/Surname

Posted on 2006-07-04
5
1,546 Views
Last Modified: 2011-10-03
Hi, just being lazy here!

Has anyone done a SQL stored procedure that will take a person's name and split it into its component parts?

E.G.

Mr Frederick A Bloggs

Would split this into:

Title: Mr
Forename: Frederick
Initials: F A
Surname: Bloggs

Ideally be intelligent enough to also spot that Mr Bloggs should only populate the Title and Surname.

I have done something similar that uses the charindex and patindex functions, I just didn't want to have to reinvent the wheel if someone else has already done it!

Thanks.
0
Comment
Question by:pauldonson
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Einstine98
ID: 17035923
we have but can't post it... IP rights of the company... but I can help you out in doing developing something if you need help.
0
 

Author Comment

by:pauldonson
ID: 17035948
OK, thanks Einstine98

This is my start (to establish the title)

select top 100 contact_ID, contact_name into #Contacts

from
TBL_Contacts

declare      @Contact_name as varchar(75),
      @Contact_title as varchar(10),
      @Contact_forename as varchar(50),
      @Contact_initials as varchar(10),
      @Contact_surname as varchar(50),
      @Contact_ID as int,
      @tmpTitle as varchar(10),
      @tmpCharIndex as int,
      @tmpContact_name as varchar(75)

while (select count(*) from #Contacts) > 0
begin

Select top 1 @Contact_ID = Contact_ID, @tmpContact_name = Contact_name from #Contacts

set @Contact_name = @tmpContact_name

--Check if Mr/Mrs/Miss/Miss/Dr is present
Set @tmpCharIndex = charindex(' ', @tmpContact_name)
If  @tmpCharIndex > 0
      begin
      Set @TmpTitle = substring(@tmpContact_name, 1, @tmpCharIndex)
      If       @TmpTitle = 'Mr' or
            @TmpTitle = 'Mrs' or
            @TmpTitle = 'Miss' or
            @TmpTitle = 'Ms' or
            @TmpTitle = 'Dr'
            Begin
            Set @Contact_title = @TmpTitle
            Set @tmpContact_name = substring(@tmpContact_name, @TmpCharIndex + 1, Len(@tmpContact_name)-@TmpCharIndex)
            end
      end
      
Print convert(varchar(10),@Contact_ID) + ',' + isnull(@Contact_name,'') + ',' + isnull(@Contact_title,'')
Delete from #Contacts where contact_id = @Contact_ID

end

drop table #Contacts



Would you say I am in the right direction? It does seem very long winded! I am just testing at the moment so the results are just being printed in SQL query analyser.
0
 
LVL 12

Accepted Solution

by:
Einstine98 earned 500 total points
ID: 17035989
before we go deep into this are using SQL 2000 or SQL 2005? if 2005... just do it using:

1. SSIS : if you can
2. CLR : Develop a function using VB or C# they are FAR better in performance and eas of use...

3. If you are stuck with 2k :-) the way I would approach is :

Create 3 user defined functions

fnMr
fnFirstname
fnLastname

You Do something like this in the query :

SELECT fnMr (Contact_Name), fnFirstName (Contact_Name), fnLastName (ContactName)
FROM MyTable

fnMR : Code that would check for ... you guessed it....etc

You can also create one function (for code managability, passing a parameter that identifies what you're looking for

fnSplitname (Contact_Name, 'Mr')

Hope this helps... if you need more help on each function let me know
0
 

Author Comment

by:pauldonson
ID: 17036007
Yep, 2000 I'm afraid. I think you are right - User-Defined Functions are the way to go!

Thanks for the tip. I'll keep you posted,.
0
 

Author Comment

by:pauldonson
ID: 17100125
Sorted it. Your tip about creating a sql function got me going. I have created different functions for each of the components.

Thanks for your help.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now