SQL Query: How to split one field into two

I have a simple query that is retrieving a name field from a table. There is NOT an individual first name and last name field; the table value is firstname;lastname. During the SELECT how can I split firstname;lastname into two columns?
kbiosAsked:
Who is Participating?
 
Leo TorresSQL DeveloperCommented:
try

DECLARE @name varchar(50)

SET @name = 'Leo;Torres'

SELECT SUBSTRING(@name,0,CHARINDEX(';',@name,0)) AS FirstName
,SUBSTRING(@name,CHARINDEX(';',@name,0)+1,Len(@name)) AS LastName

Open in new window

0
 
dsackerContract ERP Admin/ConsultantCommented:
SELECT  LEFT(name, CHARINDEX(';', name) - 1)            AS FirstName,
        SUBSTRING(name, CHARINDEX(';', name) + 1, 80)   AS LastName
FROM    YourTable

Open in new window

This makes a hard assumption that there will always be that semicolon there.
0
 
kbiosAuthor Commented:
Thanks. It worked GREAT!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.