How to truncate string using SQL server

后端 未结 6 886
一整个雨季
一整个雨季 2020-12-08 01:43

i have large string in SQL Server. I want to truncate that string to 10 or 15 character

Original string

this is test string. this is test string. thi         


        
6条回答
  •  没有蜡笔的小新
    2020-12-08 02:47

    I think the answers here are great, but I would like to add a scenario.

    Several times I've wanted to take a certain amount of characters off the front of a string, without worrying about it's length. There are several ways of doing this with RIGHT() and SUBSTRING(), but they all need to know the length of the string which can sometimes slow things down.

    I've use the STUFF() function instead:

    SET @Result = STUFF(@Result, 1, @LengthToRemove, '')
    

    This replaces the length of unneeded string with an empty string.

提交回复
热议问题