Solved

splitting names into multiple columns

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

789 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