Skip to content

Instantly share code, notes, and snippets.

@devacto
Created November 6, 2013 03:41
Show Gist options
  • Save devacto/7330518 to your computer and use it in GitHub Desktop.
Save devacto/7330518 to your computer and use it in GitHub Desktop.
Count the number of sub-strings separated by a specified delimiter.
CREATE FUNCTION substrCount(x varchar(255), delim varchar(12)) returns int
return (length(x)-length(REPLACE(x, delim, '')))/length(delim);
@devacto
Copy link
Author

devacto commented Nov 6, 2013

SELECT substrCount('/this/is/a/path', '/') as count;

+-------+
| count |
+-------+
|     4 |
+-------+

SELECT substrCount('/this/is/a/path', 'is') as count;
+-------+
| count |
+-------+
|     2 |
+-------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment