简体   繁体   English

从文本字段中查找 substring 的出现次数

[英]Find number of occurrences of substring from a text field

I have a database that unfortunately uses some Text datatype fields.我有一个不幸地使用一些Text数据类型字段的数据库。 I need to find the number of occurrences of a certain substring.我需要找到某个 substring 的出现次数。

The following code works great to find occurrences from a varchar field, but fails at many steps for text :以下代码可以很好地从varchar字段中查找匹配项,但在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() . LEN()不适用于textREPLACE()也不能。

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. DATALENGTH()不适用于此目的,因为它正在计算字节数并且不返回字符串出现次数的真实表示。

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.我已经尝试了我能想到的CAST()CONVERT()的所有组合,其中一些无法执行,而另一些则执行但返回狂野的数字,例如 67,我期望为 1。

Is there a possible way to do this for a Text field?是否有可能为Text字段执行此操作?

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.text字段转换为varchar(max)将使函数工作,但对于 substring 的每个实例,它将返回 substring 的字符数,因此要获得正确的数字,您需要除以 substring 的长度(If anyone knows why this behavior happens from a converted text field but not a varchar , I would be interested to know) (如果有人知道为什么这种行为是从转换后的text字段而不是varchar发生的,我很想知道)

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%';'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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