Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 replace last comma(,) in s string

Author  Topic 

paishwarya
Starting Member

4 Posts

Posted - 2010-11-01 : 12:11:58
I have a name column that has commas between the last n first name and few fields have a comma at the end also. How do I replace the last comma in the string to null. Eg:
HERRERA,MARITZA,
LOPEZ,DANNY
OROURKE,NICHOLAS,

I want to remove only if there is a comma at the end of the string.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-01 : 12:18:59
[code]
SELECT CASE WHEN RIGHT(Name,1)=',' THEN LEFT(Name,LEN(Name)-1) ELSE Name END
FROM Table
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

paishwarya
Starting Member

4 Posts

Posted - 2010-11-01 : 14:33:40
Thanks for your response. But I wanted to do an update on the database to remove this comma.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-02 : 03:30:26
update table
set name=LEFT(Name,LEN(Name)-1)
where RIGHT(Name,1)=','

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -