I answered a question a long time ago that may help: How to strip all non-alphabetic characters from string in SQL Server?
In that question, I show a block of code that is also helpful here (with modifications of course).
Declare @Temp VarChar(100)
Set @Temp = '(12 | 2) & 23 | 4 & 25 | (6 | 7)'
Declare @FindCharacter VarChar(10),
@ReplaceCharacter VarChar(10)
Set @FindCharacter = '2'
Set @ReplaceCharacter = '1'
While PATINDEX('%[^0-9]' + @FindCharacter + '[^0-9]%', @Temp) > 0
Set @Temp = Stuff(@Temp, PATINDEX('%[^0-9]' + @FindCharacter + '[^0-9]%', @Temp) + 1, Len(@FindCharacter), @ReplaceCharacter)
Select @Temp
Basically, this code searchs for {not a number}{value 2}{not a number}. This also means that the code will not replace the 2 if it is the first or last character of the string. The "replace" here is actually accomplished with the STUFF function. Since STUFF can only replace one string at a time, you need a while loop to cause it to do the replace multiple times.
Here is a scalar UDF to do the replacement. It will accommodate replacements even if they are at the beginning or end of the search string.
Create Function dbo.ReplaceNumber(@Input VarChar(100), @Find VarChar(10), @Replace VarChar(10))
Returns VarChar(100)
AS
Begin
Set @Input = '~' + @Input + '~'
While PATINDEX('%[^0-9]' + @Find + '[^0-9]%', @Input) > 0
Set @Input = Stuff(@Input, PATINDEX('%[^0-9]' + @Find + '[^0-9]%', @Input) + 1, Len(@Find), @Replace)
Return SubString(@Input, 2, Len(@Input)-2)
End
You can test/use the function like this:
Select dbo.ReplaceNumber('(12 | 2) & 23 | 4 & 25 | (6 | 7)','2','1')
Select dbo.ReplaceNumber('2 & 23','2','1')
Select dbo.ReplaceNumber('2 & 23','23','10')