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

前端 未结 21 2330
不思量自难忘°
不思量自难忘° 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 09:56

    If you want to do this in two steps, rather than the three of REVERSE-STUFF-REVERSE, you can have your list separator be one or two spaces. Then use RTRIM to trim the trailing spaces, and REPLACE to replace the double spaces with ','

    select REPLACE(RTRIM('a  b  c  d  '),'  ', ', ')
    

    However, this is not a good idea if your original string can contain internal spaces.

    Not sure about performance. Each REVERSE creates a new copy of the string, but STUFF is a third faster than REPLACE.

    also see this

提交回复
热议问题