2

Working through an odd issue that the moment and trying to find a solution. If we have a string variable (@string) how might we drop the last character of the string, ONLY if the last character is an s?

1 Answer 1

5

It seems a bit odd issue but here's how to do it.

DECLARE @string NVARCHAR(10)
SET @string = 'Tests'

SELECT
  CASE
    WHEN RIGHT(@string,1) IN ('s','S') THEN SUBSTRING(@string,1,LEN(@string)-1)
    ELSE @string
  END AS string
1
  • 1
    LEFT instead of SUBSTRING is slightly shorter Jan 22, 2021 at 2:09

Your Answer

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

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