Solved

FinD and Replace T-SQL Script Needed

Posted on 2006-06-20
3
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

707 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