Solved

splitting names into multiple columns

Posted on 2013-05-22
4
304 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
  • 3
4 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 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 12

Assisted Solution

by:Koen Van Wielink
Koen Van Wielink earned 250 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 12

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 12

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.

911 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

18 Experts available now in Live!

Get 1:1 Help Now