Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Splitting contact names into component Title/Forename/Surname

Posted on 2006-07-04
5
Medium Priority
?
1,619 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 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

916 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