all groups > sql server mseq > march 2005 >
You're in the

sql server mseq

group:

Need to replace Tags in Ntext


Need to replace Tags in Ntext 1idesigned
3/4/2005 3:45:02 PM
sql server mseq:
How does one properly write a replace statement when I need to find and
replace a number of different tags in a ntext column.

I am currently using (replace(col003, 'x', ''))

This only works for one character I would like to expand the replace for
additional characters.

Thanks in advance
Re: Need to replace Tags in Ntext Hugo Kornelis
3/5/2005 12:59:14 AM
[quoted text, click to view]

Hi 1idesigned,

You can nest the REPLACE function:

replace(replace(replace(col003, 'x', ''), 'y', ''), 'z', '')

Best, Hugo
--

RE: Need to replace Tags in Ntext 1idesigned
3/5/2005 2:37:01 PM
Thanks for the quick reply. I Tried the nesting but I forgot to indicate that
the column I am using the replace on is an ntext type. Do I need to modifiy
the statement to get it to work?

[quoted text, click to view]
Re: Need to replace Tags in Ntext Hugo Kornelis
3/6/2005 12:42:22 AM
[quoted text, click to view]

Hi 1idesigned,

The REPLACE function can't be used for text or ntext data.

If your data holds less than 4000 positions, you can cast it to nvarchar
before doing the replacing, as showns in the example below:

create table test (pk int not null primary key,
textcol ntext)
go
insert into test (pk, textcol)
select 1, N'This is a sample text'
insert into test (pk, textcol)
select 2, N'And another row of nonsense'
go
-- This will fail
select pk, replace(replace(textcol, 'a', ''), 'e', '')
from test
go
-- This will work; result is nvarchar(4000), not ntext
select pk, replace(replace(cast(textcol as nvarchar(4000)), 'a', ''),
'e', '')
from test
go
-- Same as above, but result converted back to ntext
select pk, cast(replace(replace(cast(textcol as nvarchar(4000)), 'a',
''), 'e', '') as ntext)
from test
go
drop table test
go


If your data may exceed 4000 position, I guess you'll either have to use
READTEXT, UPDATETEXT and WRITETEXT to handle the data in 4000-byte
chunks, or use PATINDEX, SUBSTRING and + (concatenation) to find and
replace the tags. In both cases, you'll have to implement some looping.

Best, Hugo
--

AddThis Social Bookmark Button