Tsql: how to replace a substring?

target: I have the line "1234432144". I only want to replace the first 2 4 with "10", so I would get "1231032144"

Is there any way to do this in tsql?

so far i have come up with the tsql () substring function

substring('1234432144', 4, 2) 

which draws 44. However, how to replace it in an existing line?

If I wrap a replacement function around it, it replaces all occurrences of 44 in the string.

any ideas?

early.

+4
source share
1 answer

Edited using the modified version.

 DECLARE @myStr VARCHAR(50) DECLARE @findStr VARCHAR(50) DECLARE @replaceStr VARCHAR(50) SET @myStr = '1234432144' SET @findStr = '44' SET @replaceStr = '10' SELECT STUFF(@myStr, CHARINDEX(@findStr, @myStr), LEN(@findStr), @replaceStr) 
+5
source

Source: https://habr.com/ru/post/1304783/


All Articles