Solved

Need SQL script to split single field to 2 fields between a space character

Posted on 2002-03-28
3
425 Views
Last Modified: 2012-06-27
I need a little help with a sql statement, I was told that SQL is not powerful enough to do this but I am leaning towards that fact that my knowledge of sql is just not powerful enough. I am looking for this:

I have a 2 fields in a table called Master_Name, the 1st is called firstname and the 2nd is called middlename.

     In the firstname field I have two variations of first names, just true first names (The final product I want) or firstnames along with middle initials:

David
David P

I need a sql script/statement that I can run in query analyzer that will do this;

Look at this firstname field, if is just one full name with no spaces, leave it.

Look in the firstname field If it is a group of letters (a First Name) along with  space and then characters after the space, leave the first group of letters but take the characters after the space and move them to the middlename field. (I also need the space removed.)

I am looking for a simple script something like the following , that I used to pull out extra dashes, because I am not real proficient in sql:

update dba.master_name set social_sec_no = Replace(social_sec_no,'-','')

Something very simple just to move this extra data to it's own field.

Thanks, Dave
0
Comment
Question by:DavidNPD
  • 2
3 Comments
 
LVL 3

Expert Comment

by:trouta
Comment Utility

update dba.master_name
set firstname = rtrim(substring(firstname ,1, charindex(' ', firstname ))),
middlename = rtrim(substring(firstname ,charindex(' ', @x) + 1, len(@x)))
where rtrim(firstname) like '% %'
0
 
LVL 3

Accepted Solution

by:
trouta earned 100 total points
Comment Utility
oops, try this instead


update dba.master_name
set firstname = rtrim(substring(firstname ,1, charindex(' ', firstname ))),
middlename = rtrim(substring(firstname ,charindex(' ', firstname) + 1, len(firstname)))
where rtrim(firstname) like '% %'
0
 

Author Comment

by:DavidNPD
Comment Utility
Worked great! Thanks for the help.
Dave
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

743 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

17 Experts available now in Live!

Get 1:1 Help Now