简体   繁体   English

替换为text或ntext数据类型的REPLACE

[英]alternatives to REPLACE on a text or ntext datatype

I need to update/replace the data in datatable.column. 我需要更新/替换datatable.column中的数据。 The table has a field named Content . 该表有一个名为Content的字段。 I'm using the REPLACE function. 我正在使用REPLACE功能。 Since the column datatype is NTEXT , SQL Server doesn't allow me to use the REPLACE function. 由于列数据类型是NTEXT ,SQL Server不允许我使用REPLACE函数。

I can't change the datatype because this database is 3rd party software table. 我无法更改数据类型,因为此数据库是第三方软件表。 Changing the datatype will cause the application to fail. 更改数据类型将导致应用程序失败。

UPDATE [CMS_DB_test].[dbo].[cms_HtmlText] 
SET Content = REPLACE(Content,'ABC','DEF') 
WHERE Content LIKE '%ABC%' 

I Receive this error: 我收到此错误:

Msg 8116, Level 16, State 1, Line 1 Argument data type ntext is invalid for argument 1 of replace function. 消息8116,级别16,状态1,行1参数数据类型ntext对替换函数的参数1无效。

  • Can I fix this with T-SQL? 我可以用T-SQL解决这个问题吗? Does someone have an example how to read and to loop? 有人有一个如何阅读和循环的例子吗?
  • Since this is onetime conversion, maybe I can change to another type but I'm afraid I'm messing up the data. 由于这是一次性转换,也许我可以改为另一种类型,但我担心我搞乱了数据。

There is a primary key field: name: ID - integer - it's an identity.... So I need to think about this too. 有一个主键字段:name:ID - integer - 它是一个标识....所以我也需要考虑这个问题。 Maybe set the Identity to N temporary. 也许将身份设置为N临时。

Please advise on how to achieve the REPLACE function? 请告知如何实现REPLACE功能?

Approx. 约。 3000 statements need to be updated with a new solution. 需要使用新解决方案更新3000个语句。

IF your data won't overflow 4000 characters AND you're on SQL Server 2000 or compatibility level of 8 or SQL Server 2000: 如果您的数据不会溢出4000个字符并且您使用的是SQL Server 2000或兼容级别为8或SQL Server 2000:

UPDATE [CMS_DB_test].[dbo].[cms_HtmlText] 
SET Content = CAST(REPLACE(CAST(Content as NVarchar(4000)),'ABC','DEF') AS NText)
WHERE Content LIKE '%ABC%' 

For SQL Server 2005+: 对于SQL Server 2005+:

UPDATE [CMS_DB_test].[dbo].[cms_HtmlText] 
SET Content = CAST(REPLACE(CAST(Content as NVarchar(MAX)),'ABC','DEF') AS NText)
WHERE Content LIKE '%ABC%' 

Assuming SQL Server 2000, the following StackOverflow question should address your problem. 假设SQL Server 2000,以下StackOverflow问题应该解决您的问题。

If using SQL Server 2005/2008, you can use the following code (taken from here ): 如果使用SQL Server 2005/2008,则可以使用以下代码(从此处获取 ):

select cast(replace(cast(myntext as nvarchar(max)),'find','replace') as ntext)
from myntexttable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM