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.
Is there any T-sql function that will replace the specific character with given character on specified postion of a string.Liketest1;test2; I just want to replace the last ; and final string should look like test1;test2I went through the function REPLACE but it does replaces all the placesany help is Greately appriciatedVish
SamC
White Water Yakist
3467 Posts
Posted - 2003-03-26 : 16:23:06
SET @MyString = 'test1;test2;'set @MyString = LEFT(@MyString, LEN(@Mystring)-1)That should do it.If you post the code that generates that string - there's a solution that will generate it without the last ;.Sam
REDDY
Starting Member
43 Posts
Posted - 2003-03-26 : 16:32:45
Hi Sam Thanks for your input,the code works fine if we have ; in last position but if we don't have ';' in the last postion I want to take the whole string in it's orginal form,SoWhat I want is to Just replace the ';' with '' if and only if finds ';' in last postionvish
X002548
Not Just a Number
15586 Posts
Posted - 2003-03-26 : 16:43:13
SET @MyString = 'test1;test2;' SELECT @MyString = CASE WHEN RIGHT(@MyString),1 = ';' THEN @MyString ELSE LEFT(@MyString, LEN(@Mystring)-1) ENDBrett8-)
REDDY
Starting Member
43 Posts
Posted - 2003-03-26 : 16:56:03
Brett Thats perfect...except a small change in Else case..declare @MyString varchar(100)SET @MyString = 'test1;test;' -- 'test1;test'SELECT @MyString = CASE WHEN RIGHT(@MyString,1) = ';' THEN LEFT(@MyString, LEN(@Mystring)-1) ELSE @MyString END select @MyStringOnce again I appriciate all of you for your timely HelpWith RegardsVish
X002548
Not Just a Number
15586 Posts
Posted - 2003-03-26 : 16:59:28
[homer]DoooohhhI got it backwards...good catch!![/homer]Brett8-)