Replace first occurrence of substring in a string in SQL

后端 未结 4 867
粉色の甜心
粉色の甜心 2020-11-30 13:25

I have to fetch data from a @temp table which has something like \"or ccc or bbb or aaa\" I want to replace the first occurrence into space to get something like this \" ccc

4条回答
  •  爱一瞬间的悲伤
    2020-11-30 14:19

    You can use a combination of STUFF and CHARINDEX to achieve what you want:

    SELECT STUFF(col, CHARINDEX('substring', col), LEN('substring'), 'replacement')
    FROM #temp
    

    CHARINDEX('substring', col) will return the index of the first occurrence of 'substring' in the column. STUFF then replaces this occurrence with 'replacement'.

提交回复
热议问题