Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-03-28
3
Medium Priority
?
454 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

885 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