Solved

Parsing out the MiddleName from the FirstName field.

Posted on 2007-04-10
3
276 Views
Last Modified: 2012-05-05
I have just added a MiddleName column to my table.  I need to parse out the MiddleName or Initial (Anything after the first Space in the FirstName) from the FirstName Column and Update both the FirstName and Middle Name column.
0
Comment
Question by:DigitalDan3
  • 2
3 Comments
 
LVL 14

Expert Comment

by:mherchl
ID: 18883289
update yourtable
set MiddleName = ltrim(rtrim(substring(ltrim(FirstName), charindex(' ', ltrim(FirstName)), 1+len(FirstName)- charindex(' ', ltrim(FirstName))))),
FirstName = substring(ltrim(FirstName), 0, charindex(' ', ltrim(FirstName)))
0
 

Author Comment

by:DigitalDan3
ID: 18883518
This statement worked for FirstNames that contained a MiddleName however, FirstName that did not have a Middlename or Initial were moved to middlename
0
 
LVL 14

Accepted Solution

by:
mherchl earned 500 total points
ID: 18883644
sorry for that, this should work:

update yourtable
set MiddleName = ltrim(rtrim(substring(ltrim(FirstName), charindex(' ', ltrim(FirstName)), 1+len(FirstName)- charindex(' ', ltrim(FirstName))))),
FirstName = substring(ltrim(FirstName), 0, charindex(' ', ltrim(FirstName)))
where charindex(' ', ltrim(FirstName)) <> 0
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

776 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