Solved

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

Posted on 2002-03-28
3
432 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
ID: 6905101

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
ID: 6905104
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
ID: 6905614
Worked great! Thanks for the help.
Dave
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

895 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

13 Experts available now in Live!

Get 1:1 Help Now