Search code examples
sqlt-sqldynamic-sqledi

Dynamic SQL--Nested Replace() Function


I want to replace special characters in an item_desc. This data is getting transformed into EDI, and I haven't ~ or * in the description.

Would the code below work? I have double ticks because it's dynamic SQL.

replace(replace(sd.item_desc,''*'',''''),
    sd.item_desc,''~'','''') item_desc

Solution

  • No, it won't work. Try this:

    replace(replace(sd.item_desc, ''*'', ''''), ''~'', '''') item_desc
    

    First parameter of replace is a string, as you know, so you need to provide string in outer replace, which would be string returned by the inner replace.