2

I have a table, where I need to replace some values in a column.

The database is running on SQL Server 2005.

The problem is that some of the rows contain more than 4000 characters, which is giving the REPLACE function some trouble, since it demands that I cast the first parameter to the datatype NVARCHAR, and therefore any characters exceeding 4000, is being truncated.

Is there any workaround for this, other than writing an application that handles this issue?

The query in question is:

SELECT 
   Replace(cast([Database].[dbo].[fruits].[Tekst] as NVARCHAR(MAX)), 'bananas', 'apples') 
FROM [Database].[dbo].[fruits]

The column fruits is of datatype Text

Any input appreciated.

6
  • 5
    Why would nvarchar(max) cause truncation at 4000 chars? What does this return for you? SELECT LEN(REPLACE(REPLICATE(CAST('A' AS NVARCHAR(MAX)),16000),'A','B')) Are you sure your issue isn't simply that it is getting truncated in the SSMS results? Commented Mar 9, 2011 at 12:31
  • Because the maximumvalue of nvarchar is 4000: msdn.microsoft.com/en-us/library/ms186939.aspx. The query you gave me returns the value 16000, but how can I make it iterate through a column to test for values? Replacing 'A' inside the cast with the column didn't solve the issue.. Commented Mar 9, 2011 at 12:47
  • @MadsMadsDk - No 4000 is the maximum explicit value. Using max allows it to hold up to 2GB of data (a billion double byte characters) Commented Mar 9, 2011 at 12:50
  • 2
    @madsmadsdk: the maximum for NVARCHAR(MAX) is 2 GByte of data - roughly 250 times the volume of Tolstoj's War and Peace - enough for you!?!??! Commented Mar 9, 2011 at 13:03
  • 1
    @marc_s: Only just barely enough :) Commented Mar 9, 2011 at 13:09

2 Answers 2

5

I suspect that you have just hit the limit for an individual value in SSMS.

Annoyingly it doesn't allow you to set this to be unlimited and the only way I know of displaying long text is via casting to XML as below.

select
(select Replace(cast([Database].[dbo].[fruits].[Tekst] as NVARCHAR(MAX)),'bananas','apples')  AS [processing-instruction(x)] FOR XML PATH(''), TYPE)
FROM [Database].[dbo].[fruits]
Sign up to request clarification or add additional context in comments.

Comments

3

This demonstrates how REPLACE can handle longer string

SELECT CAST(REPLICATE(N'abc', 4000) AS nvarchar(MAX)) +
               REPLICATE(N'def', 4000)  +
               REPLICATE(N'abc', 4000)

SELECT LEN(
       CAST(REPLICATE(N'abc', 4000) AS nvarchar(MAX)) +
               REPLICATE(N'def', 4000)  +
               REPLICATE(N'abc', 4000)
       ) --11997

SELECT REPLACE(CAST(REPLICATE(N'abc', 4000) AS nvarchar(MAX)) +
               REPLICATE(N'def', 4000)  +
               REPLICATE(N'abc', 4000), 'def', 'ddeeff')

SELECT LEN(
       REPLACE(CAST(REPLICATE(N'abc', 4000) AS nvarchar(MAX)) +
               REPLICATE(N'def', 4000)  +
               REPLICATE(N'abc', 4000), 'def', 'ddeeff')
       ) --15996

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.