MySQL: Count Occurances Of A String Using REPLACE and LEN Functions

in utopian-io •  7 years ago  (edited)

The MySQL command "REPLACE" may be used to determine how many times a character or string occurs in a larger text string.

SQL allows the user to count how many times a character occurs within a larger string. The process used to do this can be extended to count occurrences of a substring, with some minor changes. The SQL commands REPLACE and LEN are used.

The basic algorithm used can be extended to other languages or software, with some changes to the syntax. (The SQL function LEN and LENGTH mean the same, depending on the SQL version being used).

SQL Count Instances of a Character in a String - Algorithm

An example will be used here to illustrate the use of the REPLACE and LEN functions: "Count how many times the character "d" occurs in the string "Edward Woodward". Clearly, the answer is 4.

The length of the string "Edward Woodward" is 15. (6 for Edward, 1 for the space, and 8 for Woodward). If all instances of the letter "d" are removed, to give "Ewar Woowar", then the length of the string decreases by 4 to 11.

If the letter "o" was the one the user was interested in, then the string would decrease in length by 2, since there are two instances of the character "o". The letter "w" is slightly different, since there are two lower case "w" instances, and one upper case instance. This can also be accounted for, and will be explained later.

SQL Count Instances of a Character in a String - Syntax

Using the example of counting instances of the character "d" in the string "Edward Woodward", the user is really asking:

What is the length of the string "Edward Woodward" minus the length of "Edward Woodward" with the "d" characters all taken away? In SQL, the first part is simply

LEN("Edward Woodward")

The second part uses the REPLACE function. Basically, the letter "d" is replaced with nothing, or "": To remove the "d" characters, the syntax is

REPLACE("Edward Woodward","d","")

This will return "Ewar Woowar" as desired.

Putting it all together, the full function is then

LEN("Edward Woodward") - LEN(REPLACE("Edward Woodward","d",""))

In the case where it was desired to find all instances of "w", both upper case"W" and lower case "w", then the original string may be changed to all lower case:

LEN("Edward Woodward") - LEN(REPLACE(LOWER("Edward Woodward"),"d",""))
SQL Count Instances of a Substring in a String - Syntax

Going one step further, to find how many times a substring occurs, is also possible. For example, to count how many times the string "ward" occurs in "Edward Woodward", the same basic algorithm is used. Since replacing "ward" in the string reduces the total string length by 4, then the overall function needs to be divided by 4:

(LEN("Edward Woodward") - LEN(REPLACE("Edward Woodward","ward",""))) / LEN("ward")

or

(15 - 7) / 4

= 2

as desired.

SQL Character Count in String - Summary

Counting the number of instances of a substring in a string can be done by counting the length of the string before and after removing the substring. The syntax for both upper and lower cases, and one case only have been described using examples. (The SQL function LEN and LENGTH mean the same, depending on the SQL version being used).

The principle used can be extended to other software types such as Excel, JSL Scripts, Java etc.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Your contribution cannot be approved yet. See the [Utopian Rules]

As our supervisor @espoem suggested that you need to put the codes in the code blocks. eg: this.

You can comment once you've done the editing of the post.

(https://utopian.io/rules). Please edit your contribution to reapply for approval.

You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

Done

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @elissa I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x