Solved

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

Posted on 2002-03-28
3
435 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

813 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

10 Experts available now in Live!

Get 1:1 Help Now