MySQL String Functions
Overview
In this tutorial, we will learn about MySQL String Functions. There are many functions in MySQL that operate on strings.
MySQL String Functions
We can use string functions to calculate string length, concatenate strings, compare strings, extract sub strings of strings, search for sub strings, padding strings, trim strings, perform lowercase to uppercase conversion, etc.
String functions can be broadly divided into two categories: functions that return number and functions that return strings. Some string functions are as follows:
MySQL String Function | Description |
---|---|
CHAR_LENGTH(<string>) | The function returns the number of characters in the given string. |
INSTR(<string>, <sub-string>) | The function returns the position in the string where the sub-string occurs, or 0 if the sub-string is not in the string |
STRCMP(<string1>, <string2>) | String Compare function The function returns 0 if arguments are the same,-1 if string1 is smaller than string2, and 1 otherwise. |
CONCAT(<arg1>, [,<arg2>…., <argn>]) | The function concatenates the given arguments into one string |
REVERSE(<string>) | The function returns the string with the characters in the reverse order |
RIGHT(<string>, <length>) | The function returns the rightmost length characters of string |
LEFT(<string>, <length>) | The function returns the leftmost length characters of string |
LOWER(<string>) | Returns string with all characters in the lowercase format |
UPPER(<string>) | Returns string with all characters in the uppercase format |
LPAD(<string>, <length>, <padding>) | The function returns the string left padded with padding until it reaches the length of characters.
|
RPAD(<string>, <length>, <padding>) | The function returns the string right padded with padding until it reaches the length of characters.
|
TRIM ([[LEADING | TRAILING] <string> FROM ] <fullstring> | The function removes the string characters at the end and/or beginning of the <fullstring> . When there is no argument after the <string> it returns <string> with all leading and trailing spaces trimmed. |
SUBSTRING (<string>, <startpos> [, <chars>]) | The function returns the part of string starting at the position startpos and ending chars characters if specified, otherwise the function returns until the end of the string. |
SUBSTRING_INDEX(<string>, <delimiter>, <count>) | The function searches the string for the count occurrence of delimiter(from the left if count is positive, right if negative) and returns the characters searched through when reaching the count. |
Examples
mysql> SELECT CONCAT(‘www.’,’Testing’,’Docs’,’.com’);
+—————————————-+
| CONCAT(‘www.’,’Testing’,’Docs’,’.com’) |
+—————————————-+
| www.TestingDocs.com |
+—————————————-+
mysql> SELECT REVERSE(‘TestingDocs.com’);
+—————————-+
| REVERSE(‘TestingDocs.com’) |
+—————————-+
| moc.scoDgnitseT |
+—————————-+
—
MySQL Tutorials
MySQL Tutorials on this website:
https://www.testingdocs.com/mysql-tutorials-for-beginners/
For more information on MySQL Database: