Solved

FinD and Replace T-SQL Script Needed

Posted on 2006-06-20
3
303 Views
Last Modified: 2007-12-19
I need a SQL script to find all line breaks in a text field and replace them with the string "<br>".  Lets call the field [Users].[TextBlob].  This is for SQL server 2000.
0
Comment
Question by:creativelogic
3 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 16943492
Hi creativelogic,

How are your line breaks defined?  Line feed, carriage return, or both?

Regards,

Patrick
0
 

Author Comment

by:creativelogic
ID: 16943511
lets say both
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 16943739
Hi creativelogic,

Here is the query



-- create table Users ( i int, TextBlob text)
-- go
-- insert into Users select '1', 'Hello
-- world
-- this
-- is
-- SQL '

DECLARE @old varchar(20) ,
@new varchar(20)
SELECT @old = char(13),
@new = '<br>'

DECLARE @i int
DECLARE @txtPtr binary(16) ,
    @offset int ,
    @dellen int

SELECT @dellen = len(@old)
SELECT @txtPtr = 0

WHILE @txtPtr IS NOT NULL
BEGIN
    SELECT @txtPtr = null
    SELECT TOP 1 @i = i, @txtPtr = textptr(TextBlob)
    FROM Users
    WHERE TextBlob LIKE '%' + @old + '%'
   
    IF @txtPtr IS NOT NULL
    BEGIN
        SELECT @offset = 1
        WHILE @offset <> 0
        BEGIN
            SELECT @offset = patindex('%' + @old + '%', TextBlob)
            FROM Users
            WHERE i = @i
            --SELECT offset = @offset
           
            IF @offset > 0
            BEGIN
                SELECT @offset = @offset-1
                UPDATETEXT Users.TextBlob @txtPtr @offset @dellen @new
            END
        END -- end while
    END-- End if
END-- end while




SELECT * FROM Users


Aneesh R!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

823 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