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

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

    If for some reason your column logic is complex (case when ... then ... else ... end), then the above solutions causes you to have to repeat the same logic in the len() function. Duplicating the same logic becomes a mess. If this is the case then this is a solution worth noting. This example gets rid of the last unwanted comma. I finally found a use for the REVERSE function.

    select reverse(stuff(reverse('a,b,c,d,'), 1, 1, ''))
    

提交回复
热议问题