Solved

splitting names into multiple columns

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQl query 19 14
Finding records between to times and spanning over midnight 21 44
IN with @variable 5 20
Square brackets 4 0
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

760 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

21 Experts available now in Live!

Get 1:1 Help Now