Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (20.3k points)

How do I remove the last character in a string in T-SQL?

For example:

'TEST STRING'

to return:

'TEST STRIN'

1 Answer

0 votes
by (40.4k points)

Try the below code:

DECLARE @String VARCHAR(100)

SET @String = 'TEST STRING'

-- Chop off the end character

SET @String = 

     CASE @String WHEN null THEN null 

     ELSE (

         CASE LEN(@String) WHEN 0 THEN @String 

            ELSE LEFT(@String, LEN(@String) - 1) 

         END 

     ) END

SELECT @String

Related questions

0 votes
1 answer
asked Sep 10, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 23, 2019 in SQL by Tech4ever (20.3k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...