Solved

How do I replace strings in Text datatype without truncating to 8000 chars

Posted on 2011-09-12
11
451 Views
Last Modified: 2012-05-12
Hi,

I have a Text Datatype column and I need to replace some of the strings within them. I did try to use the REPLACE function like so:

UPDATE <Table> set textcolumn=
REPLACE(SUBSTRING(textcolumn,1,DATALENGTH(textcolumn)),'findtext','replacetext')
WHERE <Condition>


However I have now discovered that the SUBSTRING truncates to 8000 chars.

I am using SQL Server 2005.

How can I do a replace in this Text column without truncation.

Thanks,

Sam
0
Comment
Question by:SamJolly
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 5

Accepted Solution

by:
DerZauberer earned 200 total points
ID: 36523155
0
 
LVL 5

Expert Comment

by:DavidMorrison
ID: 36523190
Hi Sam


If you do an explicit conversion to varchar(max) first this should allow you to use replace across the while value.

so something like this (untested):

UPDATE <Table> set textcolumn=
REPLACE(SUBSTRING(cast(textcolumn as varchar(max)),1,DATALENGTH(textcolumn)),'findtext','replacetext')
WHERE <Condition>

does that do what you need?


Thanks

Dave
0
 

Author Comment

by:SamJolly
ID: 36523224
Thanks for the quick replies....

As long as the full contents of the Text DataType column can be guaranteed not to be truncated then that is what I need.

Sam

0
 
LVL 5

Assisted Solution

by:DavidMorrison
DavidMorrison earned 200 total points
ID: 36523240
Hi Sam, what I'd do to test would be run a select, putting a LEN around the converted column to ensure it's maintaining all the full string



Thanks

Dave
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 100 total points
ID: 36526853
>>However I have now discovered that the SUBSTRING truncates to 8000 chars. <<
Actually it does not.  SUBSTRING supports text data types as can be easily verified.  However, the correct solution as suggested previously is to convert all your code from the deprecated text data type to varchar(MAX)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:SamJolly
ID: 36526879
Hi acperkins,

Yes I have now picked up on this. My comment would have been correct for SQL2000/SqlServer7, but not for SQL Server 2005 and beyond. I have found the MAX solution works great.

Thanks,

Sam
0
 

Author Comment

by:SamJolly
ID: 36526883
I am splitting the points as 2 MAX solutions and one verification !

Thanks,

Sam
0
 

Author Closing Comment

by:SamJolly
ID: 36526887
thks. Fab help.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36526901
>>My comment would have been correct for SQL2000/SqlServer7<<
Actually no.  SUBSTRING on text data types beyond 8000 characters was also supported with SQL Server 2000.
0
 

Author Comment

by:SamJolly
ID: 36526912
Ok... I have been reading duff info....

Thanks for clarifying.

Sam
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36527124
Try this in SQL Server 2000:
-- First create a temporary table and fill some data in a text column:
CREATE TABLE #MyTable (TextCol text)

DECLARE @ColPtr binary(16),
	@Value varchar(6000)

INSERT  #MyTable
        (TextCol)
VALUES  (REPLICATE('A', 6000))

SELECT  @ColPtr = TEXTPTR(TextCol)
FROM    #MyTable

SET @Value = REPLICATE('B', 6000)

UPDATETEXT #MyTable.TextCol @ColPtr NULL 0 @Value

SET @Value = REPLICATE('C', 6000)

UPDATETEXT #MyTable.TextCol @ColPtr NULL 0 @Value


-- Next output the results from different sections using SUBSTRING
SELECT  SUBSTRING(TextCol, 10000, 40),
        SUBSTRING(TextCol, 15000, 40)
FROM    #MyTable

-- Finally drop the table
DROP TABLE #MyTable



Output:
---------------------------------------- ----------------------------------------
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now