Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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 semicolonLike 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 NewStringToo old to Rock'n'Roll too young to die.
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