The COUNT_SUBSTRINGS function counts the number of occurrences of a specified substring within a string. Note: The current implementation continues searching after shifting by the length of the substring when a match is found. For example, when str=‘ccc’ and pattern=‘cc’, the result returned is 1.
COUNT_SUBSTRINGS(<str>, <pattern>[, <start_pos>])
| Parameter | Description |
|---|---|
<str> | The string to be searched. Type: STRING |
<pattern> | The substring to match. Type: STRING |
<start_pos> | Position (1-based) at which the search starts. Type: INT. Optional |
Returns an INT type, representing the number of times the substring appears in the string.
Special cases:
SELECT count_substrings('a1b1c1d', '1');
+----------------------------------+ | count_substrings('a1b1c1d', '1') | +----------------------------------+ | 3 | +----------------------------------+
SELECT count_substrings(',,a,b,c,', ',');
+-----------------------------------+ | count_substrings(',,a,b,c,', ',') | +-----------------------------------+ | 5 | +-----------------------------------+
SELECT count_substrings('ccc', 'cc');
+--------------------------------+ | count_substrings('ccc', 'cc') | +--------------------------------+ | 1 | +--------------------------------+
SELECT count_substrings(NULL, ',');
+-----------------------------+ | count_substrings(NULL, ',') | +-----------------------------+ | NULL | +-----------------------------+
SELECT count_substrings('a,b,c,abcde', '');
+-------------------------------------+ | count_substrings('a,b,c,abcde', '') | +-------------------------------------+ | 0 | +-------------------------------------+
SELECT count_substrings('ṭṛì ḍḍumai ṭṛì ti ḍḍumannàri', 'ḍḍu', 1), count_substrings('ṭṛì ḍḍumai ṭṛì ti ḍḍumannàri', 'ḍḍu', 6);
+-----------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+ | count_substrings('ṭṛì ḍḍumai ṭṛì ti ḍḍumannàri', 'ḍḍu', 1) | count_substrings('ṭṛì ḍḍumai ṭṛì ti ḍḍumannàri', 'ḍḍu', 6) | +-----------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+ | 2 | 1 | +-----------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+
SELECT count_substrings('éèêëìíîïðñòó éèêëìíîïðñòó', 'éèê', 0), count_substrings('éèêëìíîïðñòó éèêëìíîïðñòó', 'éèê', 30);
+------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------+ | count_substrings('éèêëìíîïðñòó éèêëìíîïðñòó', 'éèê', 0) | count_substrings('éèêëìíîïðñòó éèêëìíîïðñòó', 'éèê', 30) | +------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------+ | 0 | 0 | +------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------+
SELECT count_substrings('你好,世界!你好,世界!', '世界', 0), count_substrings('你好,世界!你好,世界!', '世界', 30);
+-----------------------------------------------------------------------+------------------------------------------------------------------------+ | count_substrings('你好,世界!你好,世界!', '世界', 0) | count_substrings('你好,世界!你好,世界!', '世界', 30) | +-----------------------------------------------------------------------+------------------------------------------------------------------------+ | 0 | 0 | +-----------------------------------------------------------------------+------------------------------------------------------------------------+