In this post I will go through few of the common string operations available in MySQL database. The examples here uses HR Schema. The post SQL - Structured Query Language has instructions about the schema. Let us get started and look at the functions in detail.
Concatenate
concat function returns the string that results from concatenating the arguments. In the example below, we are producing a json like structure.
The ‘substr’ function takes as argument a string, the starting position and optionally the length.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> SELECT SUBSTRING('Quadratically',5);
+------------------------------+
| SUBSTRING('Quadratically',5) |
+------------------------------+
| ratically |
+------------------------------+
1 row in set (0.00 sec)
mysql> SELECT SUBSTRING('Quadratically',5,6);
+--------------------------------+
| SUBSTRING('Quadratically',5,6) |
+--------------------------------+
| ratica |
+--------------------------------+
1 row in set (0.00 sec)
Substring Index
The substring_index function takes the string a delimiter and a count. Internally it will split the string based on the delimiter and return parts identified by the count.