Solved

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

Posted on 2002-03-28
3
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Data Warehouse / Data Marts 4 42
SQL syntax for max(date) 3 39
MAC Dreamweaver connect to external MS SQL Server 2 40
SQL Server 2012 and core licensing 5 32
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

710 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