简体   繁体   中英

Find number of occurrences of substring from a text field

I have a database that unfortunately uses some Text datatype fields. I need to find the number of occurrences of a certain substring.

The following code works great to find occurrences from a varchar field, but fails at many steps for text :

SELECT
    KEY_FIELD,
    LEN(FIELD) - LEN(REPLACE(FIELD, 'findMe', ''))
FROM 
    TABLE
WHERE 
    FIELD LIKE '%findMe%';

LEN() does not work with text , and neither does REPLACE() .

DATALENGTH() does not work for this purpose as it is counting bytes and does not return a true representation of how many times the string occurs.

I have tried all combinations of CAST() and CONVERT() that I could think of, some of which fail to execute, while others execute but return wild numbers, like 67 where I expect a 1.

Is there a possible way to do this for a Text field?

My "best" attempt:

SELECT
    NAME,
    TEXT_FIELD,
    LEN(CONVERT(VARCHAR(max), TEXT_FIELD)) - LEN(REPLACE(CONVERT(VARCHAR(max), TEXT_FIELD), 'view', ''))
FROM 
    TESTING
WHERE 
    TEXT_FIELD LIKE '%view%';'

I made a testing table for this question to demonstrate, and the above query returns:

NAME  | TEXT_FIELD                                                                  | COUNT
------|-----------------------------------------------------------------------------|------
NAME1 | There is a review in which we view the only views that a person could view. |16
NAME2 | Search me for the term view, it will also find review.                      |8

If you would like to try to reproduce:

CREATE TABLE TESTING(
    NAME varchar(50),
    TEXT_FIELD TEXT
);
INSERT INTO TESTING VALUES('NAME1', 'There is a review in which we view the only views that a person could view.');
INSERT INTO TESTING VALUES('NAME2', 'Search me for the term view, it will also find review.');

Converting the text field to varchar(max) will make the functions work, but for each instance of the substring it will return the number of characters of the substring, so to get the correct number you need to divide by the length of the substring.(If anyone knows why this behavior happens from a converted text field but not a varchar , I would be interested to know)

Solution:

SELECT
    KEY_FIELD,
    TEXT_FIELD,
    (LEN(CONVERT(VARCHAR(max), TEXT_FIELD)) - LEN(REPLACE(CONVERT(VARCHAR(max), TEXT_FIELD), 'substring', ''))) / LEN('substring') AS 'CountOfSubstring'
FROM 
    TABLE
WHERE 
    TEXT_FIELD LIKE '%substring%';'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM