 <p>fixed it with this function:</p> <pre><code>CREATE FUNCTION [dbo].[ReplaceWithDefault] ( @InputString VARCHAR(4000) ) RETURNS VARCHAR(4000) AS BEGIN DECLARE @Pattern VARCHAR(100) SET @Pattern = '$[%]_%##%[%]$' -- working copy of the string DECLARE @Result VARCHAR(4000) SET @Result = @InputString -- current match of the pattern DECLARE @CurMatch VARCHAR(500) SET @curMatch = '' -- string to replace the current match DECLARE @Replace VARCHAR(500) SET @Replace = '' -- start + end of the current match DECLARE @Start INT DECLARE @End INT -- length of current match DECLARE @CurLen INT -- Length of the total string -- 8001 if @InputString is NULL DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001) WHILE (PATINDEX('%' + @Pattern + '%', @Result) != 0) BEGIN SET @Replace = '' SET @Start = PATINDEX('%' + @Pattern + '%', @Result) SET @CurMatch = SUBSTRING(@Result, @Start, @Len) SET @End = PATINDEX('%[%]$%', @CurMatch) + 2 SET @CurMatch = SUBSTRING(@CurMatch, 0, @End) SET @CurLen = LEN(@CurMatch) SET @Replace = REPLACE(RIGHT(@CurMatch, @CurLen - (PATINDEX('%##%', @CurMatch)+1)), '%$', '') SET @Result = REPLACE(@Result, @CurMatch, @Replace) END RETURN(@Result) END </code></pre>