Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sp_wiz
Yak Posting Veteran
55 Posts |
Posted - 2002-04-02 : 10:35:52
|
| Is it possible to replace text in an Ntext field, if so could you please give an exampleRobp |
|
|
Jay99
468 Posts |
Posted - 2002-04-02 : 10:44:43
|
USE NorthwindGOCREATE TABLE TextParts (ColA INT PRIMARY KEY, ColB TEXT)GOINSERT INTO TextParts VALUES( 1, 'Sample string START TAG Text to go END TAG Trailing text.')GODECLARE @PtrVar BINARY(16)DECLARE @InsertPos INTDECLARE @DeleteLen INT SELECT @PtrVar = TEXTPTR(ColB), @InsertPos = (PATINDEX('%START TAG%', ColB) + 9), @DeleteLen = ( PATINDEX('%END TAG%', ColB) - ( PATINDEX('%START TAG%', ColB) + 9 + 2 /* allow for blanks */ ) )FROM TextPartsWHERE ColA = 1 UPDATETEXT TextParts.ColB @PtrVar @InsertPos @DeleteLen WITH LOG 'The new text'GOSELECT * FROM TextPartsGOBurglared from BOL. Search on UPDATETEXT, display 'Changing ntext, text or image Data' . . .Jay<O> |
 |
|
|
sp_wiz
Yak Posting Veteran
55 Posts |
Posted - 2002-04-02 : 13:03:44
|
| Cheers I think this is leading me in the right directionHere is what i have done so farProblem is i can't declare a local variable that is of NTEXT DECLARE @PtrVar BINARY(16)DECLARE @InsertPos INTDECLARE @DeleteLen INTDECLARE @Finish INTDECLARE cellnet_change_cursor CURSOR FOR SELECT Cola,Colbfrom textpartsOPEN cellnet_change_cursorFETCH NEXT FROM cellnet_change_cursorWHILE @@fetch_status = 0 BEGIN set @Insertpos = 1 while @Insertpos > 0 Begin SELECT @PtrVar = TEXTPTR(colb), @InsertPos = (PATINDEX('%BT Cellnet%', colb)-1), @DeleteLen = 10 IF @InsertPOS = -1 BREAK UPDATETEXT TextParts.ColB @PtrVar @InsertPos @DeleteLen 'O2'EndFETCH NEXT FROM cellnet_change_cursorENDClose cellnet_change_cursorDeallocate cellnet_change_cursor GOSELECT * FROM TextPartsGO |
 |
|
|
Jay99
468 Posts |
Posted - 2002-04-02 : 13:22:27
|
quote: Problem is i can't declare a local variable that is of NTEXT
That is true. What do you need it for?Jay<O> |
 |
|
|
Jay99
468 Posts |
Posted - 2002-04-02 : 14:02:06
|
quote: Hi Jay Thanks for helping out on this... The code I posted just dosent work. The main idea is to search through an ntext field and replace every occurence of 'bt cellnet' with 'o2' Heres a full listing of my code
The following works . . . look mom, no cursors!!USE northwindGO drop table textpartsgoCREATE TABLE TextParts (ColA INT PRIMARY KEY, ColB TEXT) GO INSERT INTO TextParts VALUES( 1, 'Sample string BT Cellnet Text to go END TAG BT Cellnet text.') INSERT INTO TextParts VALUES( 2, 'Sample string BT Cellnet Text to go END TAG BT Cellnet text.') INSERT INTO TextParts VALUES( 3, 'Sample string BT Cellnet Text to go END TAG BT Cellnet text.') INSERT INTO TextParts VALUES( 4, 'Sample string BT Cellnet Text to go END TAG BT Cellnet text.') GO DECLARE @PtrVar BINARY(16) DECLARE @InsertPos INT DECLARE @DeleteLen INT DECLARE @Finish INTDECLARE @counter intdeclare @maxcola intselect @counter = 1, @maxcola = max(cola)from TextPartswhile @counter < = @maxcolabegin while exists ( select 1 from TextParts where ColA = @counter and patindex('%BT Cellnet%', colb) <> 0 ) begin select @ptrVar = textptr(colb), @InsertPos = (PatIndex('%BT Cellnet%', colb)-1), @deleteLen = len('BT Cellnet') from TextParts where ColA = @counter UPDATETEXT TextParts.ColB @PtrVar @InsertPos @DeleteLen 'O2' end set @counter = @counter + 1endgoSELECT * FROM TextParts GO Jay<O> |
 |
|
|
|
|
|
|
|