0
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)?

2 Answers 2

1
CREATE 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.

0

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;

https://dbfiddle.uk/gxVe5V6x

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)='/'  ;

https://dbfiddle.uk/3ztPciO_

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.