Solved

FinD and Replace T-SQL Script Needed

Posted on 2006-06-20
3
282 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
Comment Utility
Hi creativelogic,

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

Regards,

Patrick
0
 

Author Comment

by:creativelogic
Comment Utility
lets say both
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 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

15 Experts available now in Live!

Get 1:1 Help Now