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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Find and Replace only the FIRST comma

Author  Topic 

gpc44
Starting Member

35 Posts

Posted - 2013-12-18 : 08:13:03
Hi,

I'd only the one occurrence of a character in a string replace.
Excample: (this is a test, hello World 4.2 test)
Only the first comma must Replaced with semicolon
Like this=>: (this is a test; hello World 4.2 test)
First comma = semikolon :)

Best Regards
Nicole

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-12-18 : 08:25:17
declare @theString varchar(255)
set @theString = 'the first comma, second, bla bla...'

select
@theString as OrgString,
stuff(@theString,charindex(',',@theString),1,';') as NewString



Too old to Rock'n'Roll too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-12-18 : 09:02:22
Here is a modification in case of there is no comma in the string...

declare @theString varchar(255)
set @theString = 'the first comma. second. bla bla...'

select
@theString as OrgString,
stuff(@theString,charindex(',',@theString),1,';') as NewString,
case
when charindex(',',@theString)>0 then stuff(@theString,charindex(',',@theString),1,';')
else @theString
end as NewString2



Too old to Rock'n'Roll too young to die.
Go to Top of Page
   

- Advertisement -