[MDEV-26252] Function to count occurrences of a substring Created: 2021-07-27  Updated: 2021-07-27

Status: Open
Project: MariaDB Server
Component/s: None
Fix Version/s: None

Type: Task Priority: Minor
Reporter: Federico Razzoli Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: functions


 Description   

There is no trivial way to count the occurrences of a substring in a string. If you try searching Google, you'll see that many people asked how to that over the years. Generally, people suggest using expressions like this, which is terrible and painful:

CHAR_LENGTH(haystack) - CHAR_LENGTH(REPLACE(haystack, needle, '')) / CHAR_LENGTH(needle)

Surprisingly, from a quick research, no DBMS seems to have a SUBSTR_COUNT() or equivalent function. Some have REGEXP_COUNT(), but I suggest implementing a simple function for simple cases instead.

Some practical use cases for SUBSTR_COUNT() include counting the elements of a CSV and writing a CHECK constraint to validate strings in a particular format.



 Comments   
Comment by Federico Razzoli [ 2021-07-27 ]

I opened this as a bug by mistake, it should be a task. I don't know how to convert it, though.

Comment by Anel Husakovic [ 2021-07-27 ]

I did it. Thanks.

Generated at Thu Feb 08 09:43:53 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.