Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Splitting contact names into component Title/Forename/Surname

Posted on 2006-07-04
5
Medium Priority
?
1,611 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

715 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