rtrim('txt/','/')
What if we cannot use this version of the rtrim function.. How else to remove the trailing '/' from the end of a string (if present)?
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.
Sign up to join this communityCREATE TABLE TEST
(
str VARCHAR (63)
);
Populate it:
INSERT INTO test VALUES ('testing/'), ('testing');
And run the following SQL:
SELECT
CASE
WHEN LEFT(REVERSE(str), 1) != '/' THEN str
ELSE LEFT(str, (LEN(str) - 1))
END AS str
FROM test;
Result:
str
testing
testing
Fiddle available here.
There are some ways of achieving the desired result.
Another option is using STUFF
SELECT
CASE
WHEN right(str,1) <> '/' THEN str
ELSE STUFF(str,DATALENGTH(str), 1, '')
END AS str
FROM test;
If you want to update the table and remove the last /
from the string
update test
set str = STUFF(str,DATALENGTH(str), 1, '')
where right(str,1)='/' ;