Using String Functions, Part 3
Here is Part 3 of this tutorial for MySQL String Functions. Here are the links to Part 1 and 2:
- Part 1 of MySQL String Functions
- Part 2 of MySQL String Functions
Here is the list of MySQL String Functions for Part 3:
No. |
Name |
Description |
Practice |
31 | REPEAT(str,count) | Repeat a string the specified number of times
Returns a string consisting of the string str repeated count times.
See Practice #31-1.
If count is less than 1, returns an empty string. See Practice #31-2.
Returns NULL if str or count are NULL.
| |
32 | REPLACE(str,from_str,to_str) | Returns the string str with all occurrences of the string from_str
replaced by the string to_str.
REPLACE() performs a case-sensitive match when searching for from_str.
| #32 |
33 | REVERSE(str) | Returns the string str with the order of the characters reversed.
| #33 |
34 | RIGHT(str,len) | Returns the rightmost len characters from the string str, or NULL if any argument is NULL. | #34 |
35 | RPAD(str,len,padstr) | Returns a string which is formed by right-padding str with the string padstr to a length of len characters.
If the length of str is the same as len, the returned value is str right-padded by padstr. See Practice #35-1.
If the length of str is longer than len, the returned value is str shortened by len number of characters. See Practice #35-2.
If the length of str is shorter than len, the returned value is an extended version of str where str is right-padded by padstr until it reaches the length of len characters. See Practice #35-3. | |
36 | RTRIM(str) | Returns the string str with trailing space characters removed. | #36 |
37 | SOUNDEX(str) | Returns a soundex string from str. Two strings that sound almost the same should have identical
soundex strings. See Practice #37-1.
A standard soundex string is four characters long alphanumeric code, but the SOUNDEX() function can return an arbitrarily
long string. You can use SUBSTRING() on the result to get a standard soundex string. See Practice #37-2.
All non-alphabetic characters in str are ignored. All international alphabetic characters outside the
A-Z range are treated as vowels.
Limitations in MySQL version 5.1
1. This function, as of MySQL version 5.1, is intended to work well with strings that are in the
English language only. Strings in other languages may not produce reliable results.
2. This function is not guaranteed to provide consistent results with strings that use multi-byte
character sets, including utf-8. | |
38 | SPACE(N) | Returns a string consisting of N space characters. | #38 |
39 | STRCMP(expr1,expr2) | STRCMP returns 0 if the two strings are the same. See Practice #39-1.
Returns -1 if the first string is smaller than the
second string based on the current sort order. See Practice #39-2.
Returns 1 if the first string is greater than
the second string based on the current sort order. See Practice #39-3. | |
40 | SUBSTR(str,pos[,len])) | SUBSTR is a synonym for SUBSTRING.
| |
41 | SUBSTRING(str,pos[,len]) | Returns a substring starting from the specified position.
SUBSTRING is a synonym for MID.
There are two main forms of using SUBSTRING:
1. SUBSTRING(str,pos[,len])
In this form, len is an optional argument. If it's omitted, SUBSTRING returns
a substring from string str starting at position pos. See Practice #41-1.
If len presents, SUBSTRING returns a substring of len characters long from string str, starting
at position pos. See Practice #41-2.
2. SUBSTRING(str FROM pos [FOR len])
In this form, FOR len is an optional argument. If it's omitted, SUBSTRING returns
a substring from string str starting at position pos. See Practice #41-3.
If FOR len presents, SUBSTRING returns a substring of len characters long from string str, starting
at position pos. See Practice #41-4.
Using negative value for pos
It is also possible to use a negative value for pos. In this case,
the beginning of the substring is pos characters from the end of
the string, rather than the beginning. See Practice #41-5.
A negative value may be used for pos in any of the two forms of this function.
| |
42 | SUBSTRING_INDEX(str,delim,count) | Returns the substring from string str before count occurrences of the delimiter delim.
If count is positive, everything to the left of the final delimiter (counting from the left)
is returned. See Practice #42-1.
If count is negative, everything to the right of the final delimiter (counting from the right)
is returned. See Practice #42-2.
SUBSTRING_INDEX() performs a case-sensitive match when searching for delim. | |
43 | TRIM(str) | Remove leading and/or trailing spaces.
There are 5 different ways to use TRIM() function:
1. TRIM(str)
Trim both leading and trailing spaces. See Practice #43-1.
2. TRIM(LEADING remstr FROM str)
Only trim leading spaces. See Practice #43-2.
3. TRIM(TRAILING remstr FROM str)
Only trim trailing spaces. See Practice #43-3.
4. TRIM(BOTH remstr FROM str)
Trim both leading and trailing spaces. See Practice #43-4.
5. TRIM(remstr FROM str)
Trim both leading and trailing spaces. See Practice #43-5. | |
44 | UCASE(str) | UCASE is synonym for UPPER function. | |
45 | UNHEX(str) | UNHEX interprets each pair of hexadecimal digits in the argument as a number and converts it to the character represented by the number.
UNHEX performs the inverse operation of HEX(). | #45 |
46 | UPPER(str) | Returns the string str with all characters changed to uppercase according to the current character set mapping.
The default is latin1 (cp1252 West European).
UPPER is synonym for UCASE function. | #46 |
Practice #31-1: Using REPEAT string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT REPEAT('Geeks', 3);
Query result set - 1 row returned:
Practice #31-2: Using REPEAT string function with negative number
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT REPEAT('Geeks', -2);
Query result set - 1 row returned:
Practice #32: Using REPLACE string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT REPLACE('GeeksEngine', 'Geeks', 'Search');
Query result set - 1 row returned:
Practice #33: Using REVERSE string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT REVERSE('GeeksEngine');
Query result set - 1 row returned:
Practice #34: Using RIGHT string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT RIGHT('GeeksEngine', 6);
Query result set - 1 row returned:
Practice #35-1: Using RPAD function with same len and pos value
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT RPAD('web',7,'site');
Query result set - 1 row returned:
Practice #35-2: Using RPAD() function with shorter len value
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT LPAD('web',2,'site');
Query result set - 1 row returned:
Practice #35-3: Using RPAD() function with longer len value
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT RPAD('web',11,'site');
Query result set - 1 row returned:
Practice #36: Using RTRIM string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
Query result set - 1 row returned:
Practice #37-1: Using SOUNDEX function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT SOUNDEX('World');
SELECT SOUNDEX('Word');
Query result set - 1 row returned:
Query result set - 1 row returned:
Practice #37-2: Using SOUNDEX function and LENGTH function to get standard soundex string
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT SOUNDEX('World Red Cross');
SELECT SUBSTRING(SOUNDEX('World Red Cross'), 1, 4);
SELECT SOUNDEX('Word Red Grass');
SELECT SUBSTRING(SOUNDEX('Word Red Grass'), 1, 4);
Query result set - 1 row returned for non-standard soundex string:
Query result set - 1 row returned for standard soundex string:
Query result set - 1 row returned for non-standard soundex string:
Query result set - 1 row returned for standard soundex string:
Practice #38: Using SPACE string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT CONCAT('Geeks', SPACE(5), 'Engine') AS show_space;
Query result set - 1 row returned:
Practice #39-1: Using STRCMP string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT STRCMP('world', 'world');
SELECT STRCMP('world', 'WORLD');
Query result set - 1 row returned:
Query result set - 1 row returned:
Practice #39-2: Using STRCMP string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT STRCMP('worldcup', 'world');
Query result set - 1 row returned:
Practice #39-3: Using STRCMP string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT STRCMP('world', 'worldcup');
Query result set - 1 row returned:
Practice #41-1: Using SUBSTRING function without len argument
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT SUBSTRING('GeeksEngine',6);
Character index position for string GeeksEngine:
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
G |
e |
e |
k |
s |
E |
n |
g |
i |
n |
e |
Query result set - 1 row returned:
Practice #41-2: Using SUBSTRING function with len argument
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT SUBSTRING('GeeksEngine',8,9);
Character index position for string GeeksEngine:
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
G |
e |
e |
k |
s |
E |
n |
g |
i |
n |
e |
Query result set - 1 row returned:
Practice #41-3: Using SUBSTRING(str FROM pos)
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT SUBSTRING('GeeksEngine' FROM 6);
Character index position for string GeeksEngine:
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
G |
e |
e |
k |
s |
E |
n |
g |
i |
n |
e |
Query result set - 1 row returned:
Practice #41-4: Using SUBSTRING(str FROM pos FOR len)
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT SUBSTRING('GeeksEngine' FROM 8 FOR 9);
Character index position for string GeeksEngine:
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
G |
e |
e |
k |
s |
E |
n |
g |
i |
n |
e |
Query result set - 1 row returned:
Practice #41-5: Using SUBSTRING with negative pos value
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT SUBSTRING('GeeksEngine', -6, 3);
Character index position for string GeeksEngine:
11 |
10 |
9 |
8 |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
G |
e |
e |
k |
s |
E |
n |
g |
i |
n |
e |
Query result set - 1 row returned:
Practice #42-1: Using SUBSTRING_INDEX string function for positive count value
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT SUBSTRING_INDEX('www.GeeksEngine.com', '.', 2);
Query result set - 1 row returned:
Practice #42-2: Using SUBSTRING_INDEX string function for negative count value
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT SUBSTRING_INDEX('www.GeeksEngine.com', '.', -2);
Query result set - 1 row returned:
Practice #43-1: Using TRIM string function to trim both leading and trailing spaces
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT CONCAT('Geeks', TRIM(' Engine '));
Query result set - 1 row returned:
Practice #43-2: Using TRIM string function to trim leading characters
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT TRIM(LEADING 'xx' FROM 'xxxGeeksEngine');
Query result set - 1 row returned:
Practice #43-3: Using TRIM string function to trim trailing characters
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT TRIM(TRAILING 'xx' FROM 'GeeksEnginexxx');
Query result set - 1 row returned:
Practice #43-4: Using TRIM to remove both leading and trailing characters
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT TRIM(BOTH 'xx' FROM 'xxxGeeksEnginexx');
Query result set - 1 row returned:
Practice #43-5: Using TRIM to remove both leading and trailing characters without specifying BOTH
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT TRIM('xx' FROM 'xxxGeeksEnginexx');
Query result set - 1 row returned:
Practice #45: Using UNHEX string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT UNHEX('4765656B73');
SELECT 0x4765656B73;
Query result set - 1 row returned:
Query result set - 1 row returned:
Practice #46: Using UPPER string function
Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page.
SELECT UPPER('geeksengine');
Query result set - 1 row returned:
Other tutorials in this category
1. Using String Functions, Part 1
2. Using String Functions, Part 2
3. Using MySQL Date and Time Functions, Part 1
4. Using MySQL Date and Time Functions, Part 2
5. Using MySQL Date and Time Functions, Part 3
6. Using MySQL Control Flow Functions
7. Using MySQL Conversion Functions
8. Using MySQL Comparison Functions
9. Using Aggregate Functions in MySQL
10. MySQL RANK() function
11. MySQL DENSE_RANK() function
12. MySQL ROW_NUMBER() function
13. Compare RANK(), DENSE_RANK(), and ROW_NUMBER() function
14. Using LIMIT and OFFSET to Page Thru Query Result in MySQL
15. Using LIMIT, SQL_CALC_FOUND_ROWS and FOUND_ROWS() to Page Thru Query Result in MySQL
16. Use LEAST function to find the SMALLEST value from multiple arguments
17. How to make case-sensitive comparison in MySQL
18. Use GREATEST function to find the LARGEST value from multiple arguments
19. MySQL MID() function with examples
20. MySQL LOCATE() function with examples
21. MySQL SUBSTR() function with examples
22. MySQL POSITION() function with examples
23. How to use MySQL REPLACE function in SELECT and UPDATE queries
Back to Tutorial Index Page