?
Solved

splitting names into multiple columns

Posted on 2013-05-22
4
Medium Priority
?
313 Views
Last Modified: 2013-05-26
Hello I have the following names in one column

SPOORS, KYM DIANNE
MCKINLEY, RHONDA JEAN
COLLINS, ISABEL MERLE
WALLACE, CHRISTINA THERESE
LEACH, MICHAEL HUGH
LEACH, VALERIE JUNE
CLANCY, MAVIS EVELYN
PIZER, DOROTHY LILA
HAYGARTH, MARGARET LINDA
BRAYBROOK, MAURICE COLIN

I need to split them into

firstname
KYM DIANNE

lastname
SPOORS

into columns i have created in the table. I cannot remember..
0
Comment
Question by:Amanda Walshaw
[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
4 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 1000 total points
ID: 39189782
there might be some uexpected issues with names, but try this
select
  substring(full_name,1, charindex(',', full_name + ',' )-1) as lastname
, substring(full_name,len(substring(full_name,1, charindex(',', full_name + ',' )-1))+2,800) as firstnames
from table1

Open in new window

0
 
LVL 13

Assisted Solution

by:Koen Van Wielink
Koen Van Wielink earned 1000 total points
ID: 39189787
Hi Flyfish,

Another one for you. Hope it doesn't post 3 times this time...

Insert into newtable
(Lastname,
Firstname)

Select		ltrim(rtrim(substring(Fullname,0,patindex('%,%',Fullname)))) as 'Lastname'
		,	ltrim(rtrim(Substring(Fullname,patindex('%,%',Fullname) + 1,len(fullname) - patindex('%,%',fullname)))) as 'Firstname'
from Oldtable

Open in new window


Replace newtable with your new table name, and oldtable with the original table name.

Rgds,

Kvwielink
0
 
LVL 13

Expert Comment

by:Koen Van Wielink
ID: 39189790
Actually after seeing Paul's post it's probably better to replace Patindex with Charindex:

Insert into newtable
(Lastname,
Firstname)

Select		ltrim(rtrim(substring(Fullname,0,charindex(',',Fullname)))) as 'Lastname'
		,	ltrim(rtrim(Substring(Fullname,charindex(',',Fullname) + 1,len(fullname) - charindex('%,%',fullname)))) as 'Firstname'
from oldtable

Open in new window

0
 
LVL 13

Expert Comment

by:Koen Van Wielink
ID: 39198432
Just one more comment Flyfish. I'd still use the LTRIM and RTRIM functions around the name exctraction formulas to get rid of any leading or trailing spaces in your data. This could cause trouble in the future. I think it already showed in another question you posted where my solution (although not the best one, I'll admit that...) did not give the correct upper and lower case letters because spaces were throwing off the lenght of the field.
Just my 2 cents worth...
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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

800 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