Solved

splitting names into multiple columns

Posted on 2013-05-22
4
311 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 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 13

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 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

724 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