Working With Common MySQL String Functions

MySQL String Functions

MySQL has many built in functions for dealing with strings. Many times, we can perform these operations using whatever programming language we happen to be using with MySQL such as PHP. Other times however, it is really helpful to be able to manipulate strings right in our MySQL statements. In this episode we’ll take a look at working with strings and doing things like setting them to all upper or lowercase, reversing their order, replacing characters with other characters, gluing strings together with concatenation, and much more. Let’s get right to it!

Strings in MySQL

Likely if you’re reading an article on this website, you are familiar with PHP and it’s many string functions. MySQL also has some useful ways to operate on strings. Let’s look at a few of them now.

Using the lower and upper functions

When we make a select query on the database, we can apply a lower or upper function to string data. In this example, we’ll get all the first names of our friends in lower case with their last names in uppercase.

Repeating String with MySQL

Though it may seem like a strange use case, you can cause a string to be repeated in MySQL. The repeat() function takes the string to be repeated as the first parameter followed by the number of times to repeat it as the second parameter. Here is an example:

Replacing Output Data

We can replace a piece of a string with another piece of text. This works similar to a str_replace type function from PHP. As an example, we’ll replace the letter a with the string BOO in all of the first names from our friends table. Note that these are not actually modifying the underlying data, but rather making the change to the data which is output. We can see this by selecting all data from the friends table and we can see that everything is still in tact and as good as new.

Reversing Data

By using the reverse() function, we can easily reverse the output of a query. Here, we’ll retrieve all first names and reverse their letters:

String Concatenation with MySQL

String Concatenation is the way in which we can combine strings together. Likely you are quite familiar with this as both JavaScript and PHP use string concatenation all the time. In MySQL we can do so pretty easily as well. Let’s try a query:

Cool! We were able to select the Olivios and have a nice formatted output by sticking together their first and last names with a space in between them.

We can take it a step further with the concat_ws() function. With this function, we can specify the separator and get as many fields as we like. Let’s test it out with a custom separator here:

Getting First and Last parts of a String

We can use the left() and right() functions to do just that. Each function takes the column in question as the first parameter, and the number of characters to return as the second parameter. We can try this on the address column of our houses table. We’ll grab the first 5 characters starting from the left and the first 7 characters starting from the right. Let’s check it out:

Returning Length of Strings

By using the length() function we can check the length of the string in our various columns. Here we’ll check the length of all of the first and last names in our friends table. The second query is to simply display all of the names so we can confirm that yep, the length returned for all of our data is correct.

MySQL String Summary

As you can see, there are many ways to work with strings in MySQL. The ability to format and mold the data to our needs is very useful in MySQL. An added benefit is that unless you want to change data in your tables with insert and update statements, all of underlying data will stay in tact nicely.