How to replace first and last character of column in sql server?

后端 未结 9 2739
名媛妹妹
名媛妹妹 2021-02-19 11:43

I have a database column and its give a string like ,Recovery, Pump Exchange,.

I want remove first and last comma from string.

Expected Result :

9条回答
  •  南方客
    南方客 (楼主)
    2021-02-19 11:55

    This is an old post but I've decided to add my solution as it will remove the first and last commas of a comma separated string without removing the other commas and also will work with strings that don't start with a comma:

    DECLARE @words VARCHAR(50) = ',Recovery, Pump Exchange,'
    SELECT REPLACE(SUBSTRING(@words , 1, 1),',','') + SUBSTRING(@words, 2, LEN(@words)-2) + REPLACE(SUBSTRING(@words, LEN(@words), 1), ',', '')
    

提交回复
热议问题