Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unstring name into new table 1

Status
Not open for further replies.

Mbroad

Programmer
Feb 26, 2004
49
US
Hi--I need to insert the name fields from table 1 to table 2. The name field in table 1 is a name like "Jones Marvin" (full name). The name fields on table 2 are first-name and last-name. How can I unstring the complete name into separate name fields?
Thanks,
Mark

Government Coder Mark
 
Is there a method I can use within the SQL statement itself? Thanks---

Government Coder Mark
 
Thank you for your response. Sorry to be picky, but I do not need to find the starting position of a specific string. What I need to do is to be able to unstring the first & last-name from one field into two separate fields. Perhaps I am misunderstanding, but I don't see how PATINDEX or CHARINDEX does this.
thank you

Government Coder Mark
 
you can also do left twice with reverse in the second part

declare @name varchar(50)
select @name ='Bill Gates'
select LEFT(@name,CHARINDEX(' ',@name)-1) as FirstName,
REVERSE(LEFT(REVERSE(@name),CHARINDEX(' ',@name))) as LastName

Denis The SQL Menace
SQL blog:
Personal Blog:
 
SQLDEnis: Thank you very much. I am an SQL novice so I can't follow how to implement this for my particular process. Your example uses a hard-coded name where my process will read a file containing a name. Can you explain what each line is doing? Thanks very much

Government Coder Mark
 
Assuming your column name is FullName use this

select LEFT(FullName,CHARINDEX(' ',FullName)),
SUBSTRING(FullName,CHARINDEX(' ',FullName), (Len(FullName) -CHARINDEX(' ',FullName))+1)
From YourTable

or you can take this approach using Parsename

declare @name varchar(50)
select @name ='Bill Gates'
select PARSENAME(REPLACE(@name,' ','.'),2) as FirstName,
PARSENAME(REPLACE(@name,' ','.'),1) as LastName


so then your statement would be this

select PARSENAME(REPLACE(FullName,' ','.'),2) as FirstName,
PARSENAME(REPLACE(FullName,' ','.'),1) as LastName
From YourTable

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top