|
|
Line 1: |
Line 1: |
| Count occurrences of a string | | #REDIRECT [[Count occurrences of a word in string]] |
| | |
| == MySQL ==
| |
| <pre>
| |
| -- Count occurrences of a string: .
| |
| SET @input = "www.google.com";
| |
| SET @separator = ".";
| |
| SELECT (LENGTH(@input ) - LENGTH(REPLACE(@input , @separator, ""))) / LENGTH(@separator) AS count_of_separator;
| |
| -- expected result: 2
| |
| | |
| -- Count occurrences of a string: og
| |
| SET @input = "www.google.com";
| |
| SET @separator = "og";
| |
| SELECT (LENGTH(@input ) - LENGTH(REPLACE(@input , @separator, ""))) / LENGTH(@separator) AS count_of_separator;
| |
| -- expected result: 1
| |
| </pre>
| |
| | |
| | |
| == References ==
| |
| * [http://stackoverflow.com/questions/12344795/count-the-number-of-occurences-of-a-string-in-a-varchar-field mysql - Count the number of occurences of a string in a VARCHAR field? - Stack Overflow] {{access | date = 2016-09-21}}
| |
| | |
| [[Category:Data transformation]] [[Category:Data Science]] [[Category:MySQL]]
| |