I needed to update/replace the data in a column. The table has a field named
PageContent. I'm using the REPLACE function. Since the column datatype is NTEXT, SQL Server doesn't allow to use the REPLACE function.
Example
UPDATE
[dbo].[DynamicPages]
SET [PageContent] = Replace([PageContent], 'from', 'to')
I receive this Error
Msg 8116, Level 16, State 1, Line 2
Argument data type ntext is invalid for argument 1 of replace function.
Solution
If your data won't overflow 4000 characters AND you're on SQL Server 2000 or compatibility level of 8 or SQL Server 2000:
UPDATE [dbo].[DynamicPages]
SET [PageContent] = CAST(REPLACE(CAST([PageContent] as NVarchar(4000)),@From,@To) AS NText)
WHERE
[SiteID] = @SiteId
For SQL Server 2005+:
UPDATE [dbo].[DynamicPages]
SET [PageContent] = CAST(REPLACE(CAST([PageContent] as NVarchar(MAX)),@From,@To) AS NText)
WHERE
[SiteID] = @SiteId