There is a column of datatype TEXT in my table. Now, I need to find the number of occurrences of a string in that TEXT field. I have already created a Full-text index on that table. But I don't know how to proceed further. I already found ways to count string occurrences for VARCHAR. But they cannot be applied AS IS to TEXT field. Any suggestions?
views:
29answers:
2
A:
convert that(Text) field to varchar and find the count
e.g : convert(varchar(200),textColumn)
anishmarokey
2010-09-23 12:05:16
The size limit for varchar is 8000. The data stored in each row is more than that.
Anurag
2010-09-23 12:09:54
varchar(MAX) you can try..
anishmarokey
2010-09-23 13:01:58
+3
A:
Try this:
declare @searchString varchar(max);
set @searchString = 'something';
declare @textTable table (txt text);
insert into @textTable ( txt )
values ( 'something that has something 2 times' )
select
(
datalength(txt) -
datalength(replace(cast(txt as varchar(max)), @searchString, ''))
)
/datalength(@searchString) [Count]
from @textTable as tt
Note that casting as varchar(max)
won't truncate your text
column as the max length of varchar(max) is 2^31-1 bytes or 2Gb.
Denis Valeev
2010-09-23 12:07:24