The World Of String Functions
Last updated
Last updated
So far we've only added or removed data. Sometimes we might want to perform operations on that data. For example, maybe I want to concatenate first_name
and last_name
.
This section will deal with string functions specifically. All of MySQL's string functions can be found .
But here's the most popular string functions you'll use 99% of the time:
Pro tip: All of the below string functions can be combined by nesting one inside another.
Use CONCAT
alongside a command like SELECT
to combine strings together.
Note: We provide an alias because the new table will default to an ugly header titled CONCAT(first_name, ' ', last_name)
.
Pro tip: CONCAT_WS
is a shortcut variant that adds a separator between every column value. So CONCAT_WS(' - ', first_name, last_name, age)
will return the string '<first_name> - <last_name> - <age>'
.
Use SUBSTRING
to select portions of a string, e.g., the last 3 characters.
Use REPLACE
to replace a portion of a string with another string value.
Use REVERSE
to literally just reverse a string from, say, 'Hello World'
to 'dlroW olleH'
.
Use CHAR_LENGTH
to tell you how many characters are in a given string.
Use UPPER
and LOWER
to change the case of a string.