SQL Server find and replace in TEXT field

后端 未结 1 1545
南笙
南笙 2020-12-25 10:40

I have a database in SQL Server 2005 that was brought up from SQL Server 2000 and is still using TEXT type fields instead of varchar(max).

I need to find and replace

相关标签:
1条回答
  • 2020-12-25 11:01

    I finally figured it out. It was buried in the comments to the article jfrobishow published. Thank you SO much.

    Here is the whole response that led me to the solution:

    quote:Originally posted by fredclown

    If you use SQL 2005 you can use replace with a text type. All you have to do is the below ...

    field = replace(cast(field as varchar(max)),'string' ,'replacement')

    Easy as pie.

    Two thumbs up to Fredclown!!! command work like a charm for me as well. This is what I wrote my Update statement to Find and Replace in a Text field in SQL server 2005 database

    UPDATE TableName SET DBTextField = REPLACE(CAST(DBTextField AS varchar(MAX))
                                                   ,'SearchText', 'ReplaceText')
    FROM TableName
    WHERE CHARINDEX('SearchText',CAST(DBTextField as varchar(MAX)))>0
    

    Note:that this may truncate the size of you dbfield , but if is a long text column make it nvarchar(max) and you should not get none truncation!

    0 讨论(0)
提交回复
热议问题