How do I do a simple 'Find and Replace" in MsSQL?

前端 未结 5 650
别那么骄傲
别那么骄傲 2020-12-12 23:12

Question is pretty self explanitory. I want to do a simple find and replace, like you would in a text editor on the data in a column of my database (which is MsSQL on MS Win

5条回答
  •  死守一世寂寞
    2020-12-12 23:49

    This pointed me in the right direction, but I have a DB that originated in MSSQL 2000 and is still using the ntext data type for the column I was replacing on. When you try to run REPLACE on that type you get this error:

    Argument data type ntext is invalid for argument 1 of replace function.

    The simplest fix, if your column data fits within nvarchar, is to cast the column during replace. Borrowing the code from the accepted answer:

    UPDATE YourTable
    SET Column1 = REPLACE(cast(Column1 as nvarchar(max)),'a','b')
    WHERE Column1 LIKE '%a%'
    

    This worked perfectly for me. Thanks to this forum post I found for the fix. Hopefully this helps someone else!

提交回复
热议问题