MySQL Substring Function
MySQL substring is a MySQL string function that gives you the ability to query and return from a string, a substring. For instance, if you want to grab a small piece of text from a larger piece, the substring function is the easiest way to do it. The syntax of this MySQL string function differs depending on how you want to use it.
Below we look at the different ways of using the function substring and what the different syntax will give you.
This first example shows the most basic usage of substring syntax as follows:
In the case above, the “str” represents the string while “pos” represents the position from where the substring will start. To see the function in action, let us use a real-life function below:
The result for the above function will be: and dogs
Simply put, the function starts at the sixth position and picks a substring from the string Cats and dogs. In our second example below, we will make it a bit more complex by using the FROM clause. The syntax with the FROM clause takes the format:
The FROM clause, in this case, is borrowed from standard SQL and the syntax does not use any commas. Using the same sample we used in our basic syntax, let us change the syntax by using the FROM clause:
The result, in this case, is the same as in the first statement. There are many other ways to use this function and among them are:
Specifying substring length:
The result for this is “and” as it specifies to start from the position six and only take 3 strings forward.
Specifying length using FROM and FOR:
The result for this is the same as with the ‘len’ specifier.
The substring function also allows you to count backward.
The result for the backward count, in this case, returns “d dogs”.
In all the functions we above, there are two synonyms for SUBSTRING that you can use to yield similar results. These are “SUBSTR()” and “MID()”.
Working With MySQL SUBSTRING_INDEX()
Another MySQL string function is MySQL SUBSTRING_INDEX which allows you to get from a substring from a string. However, with MySQL SUBSTRING_INDEX, you have to specify the number of occurrences of the delimiter before which the substring will be taken.
The syntax for MySQL SUBSTRING_INDEX is:
In the syntax above, ‘str’ represents the string from which you will extract a substring while the delimiter is a string whose function is acting as a delimiter. When searching for the delimiter, the match that the function performs is case-sensitive.
The last character, ‘n’, represents an integer that specifies how many times the delimiter occurs. This integer can either be positive – where it returns characters starting from the left of a string to the number it specifies, or negative – where the characters returned start from the extreme right to the nth value of delimiter occurrences.
For instance, assuming we have the function as below:
In the example above, both the delimiter and the ‘n’ value are 1. In this case, the function will return all the characters up to the first occurrence of the delimiter1 as with the result being – ‘He’.
If we were to maintain the delimiter and 1 and only vary the n value to specify the number of times the delimiter occurs, we would have:
If we were to use negative occurrences in the above functions, we would have different results.
The results, in this case, would be:
Result1 = d
Result2= o World
Result3 = lo World
With this knowledge, you can now use the substring function in MySQL to get a substring from a string. For even better functionality, you can specify the number of occurrences of the delimiter before which to get a substring from a string.