Home All Groups Group Topic Archive Search About

Splitting 1 column into 2

Author
12 Aug 2005 7:51 PM
Joey Martin
Pretty basic but I cannot figure it out.

Column:
FULLNAME (lname, fname)

Needs to be

FNAME
LNAME

Should split at the comman. I already have all the columns created; just
need to SQL command to make it happen. Thanks!!



*** Sent via Developersdex http://www.developersdex.com ***

Author
12 Aug 2005 8:31 PM
Brian Lawton
Assuming that all of your data takes the same format and that there are no
other embedded commas, give these a try.

select left(@FullName, charindex(',', @FullName) - 1) as LastName
     , right(@FullName, charindex(',', @FullName) - 2) as FirstName1
     , ltrim(right(@FullName, charindex(',', @FullName) - 1)) as
FirstName2 -- if the number of spaces vary

--
--Brian
(Please reply to the newsgroups only.)


Show quote
"Joey Martin" <j***@infosmiths.net> wrote in message
news:eX6TNd3nFHA.572@TK2MSFTNGP15.phx.gbl...
>
>
> Pretty basic but I cannot figure it out.
>
> Column:
> FULLNAME (lname, fname)
>
> Needs to be
>
> FNAME
> LNAME
>
> Should split at the comman. I already have all the columns created; just
> need to SQL command to make it happen. Thanks!!
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
12 Aug 2005 10:07 PM
Joey Martin
Sorry, may not have been clear enough. I need to update FNAME1 and
LNAME1 from FULLNAME

Sample of FULLNAME is Doe, John

End result should be
LNAME1=Doe
FNAME1=John

Tablename is customerdata



*** Sent via Developersdex http://www.developersdex.com ***
Author
14 Aug 2005 11:55 AM
Hugo Kornelis
On Fri, 12 Aug 2005 12:51:58 -0700, Joey Martin wrote:

Show quote
>
>
>Pretty basic but I cannot figure it out.
>
>Column:
>FULLNAME (lname, fname)
>
>Needs to be
>
>FNAME
>LNAME
>
>Should split at the comman. I already have all the columns created; just
>need to SQL command to make it happen. Thanks!!

Hi Joey,

This should work:

UPDATE MyTable
SET    LName = LEFT(FullName, CHARINDEX(', ', FullName) - 1),
       RName = RIGHT(FullName, CHARINDEX(' ,', REVERSE(FullName)) - 1)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

AddThis Social Bookmark Button