Solved

Delete Characters After a Space in a String

Posted on 2009-05-05
4
992 Views
Last Modified: 2012-05-06
I have had a quick search but can't seem to get a working method of this, and I can't imagine it is the most difficult thing to do.
I have a list of about 1000 names from a client, all in the 'surname, forename' format.
I am loading them into a database but want them to be in the 'Forename Surname' format.
I have managed to get some working code to get the forename only but now need something that will get the surname only, and a way to do that is get delete all characters after and including the comma.

Cheers in advance
0
Comment
Question by:Answering4uAnt
  • 2
4 Comments
 
LVL 4

Author Comment

by:Answering4uAnt
ID: 24303346
sorry topic title slightly wrong, I want to delete all characters after a comma but imagine the method will be pretty much the same

Cheers
0
 
LVL 6

Accepted Solution

by:
bokist earned 250 total points
ID: 24303392
Try this way :

update my_table set name_field = substring(name_field, charindex(',', name_field) +1, 50) + ' ' +
                                                       substring(name_field, 1, charindex(',', name_field) -1)  
0
 
LVL 4

Author Comment

by:Answering4uAnt
ID: 24303409
Thanks, that did the job just fine, it did leave an extra space at the start which I have managed to remove easily enough

cheers
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24303415
declare @name varchar(50)
set @name='Ritesh,Shah'
select replace(@name,substring(@name,charindex(',',@name),len(@name)),'')
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
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.
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…

808 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