Remove the last character in a string in T-SQL?

前端 未结 21 2375
不思量自难忘°
不思量自难忘° 2020-12-07 09:22

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

For example:

\'TEST STRING\'

to return:

\'TES         


        
21条回答
  •  情书的邮戳
    2020-12-07 10:13

    My answer is similar to the accepted answer, but it also check for Null and Empty String.

    DECLARE @String VARCHAR(100)
    
    SET @String = 'asdfsdf1'
    
    -- If string is null return null, else if string is empty return as it is, else 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
    

提交回复
热议问题