Example
Reverse a string:
SELECT REVERSE("SQL Tutorial");
Definition and Usage
The REVERSE() function reverses a string and returns the result. Continue reading MySQL REVERSE Function
Reverse a string:
SELECT REVERSE("SQL Tutorial");
The REVERSE() function reverses a string and returns the result. Continue reading MySQL REVERSE Function
Replace “SQL” with “HTML”:
SELECT REPLACE("SQL Tutorial", "SQL", "HTML");
The REPLACE() function replaces all occurrences of a substring within a string, with a new substring.
Note: This function performs a case-sensitive replacement. Continue reading MySQL REPLACE Function
Repeat a string 3 times:
SELECT REPEAT("SQL Tutorial", 3);
The REPEAT() function repeats a string as many times as specified. Continue reading MySQL REPEAT Function
Search for “3” in string “Iampsp.com”, and return position:
SELECT POSITION(“3” IN “Iampsp.com”) AS MatchPosition;
The POSITION() function returns the position of the first occurrence of a substring in a string.
If the substring is not found within the original string, this function returns 0.
This function performs a case-insensitive search. Continue reading MySQL POSITION Function
Extract a substring from a string (start at position 5, extract 3 characters):
SELECT MID("SQL Tutorial", 5, 3) AS ExtractString;
The MID() function extracts a substring from a string (starting at any position).
Note: The position of the first character in the string is 1.
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
S | Q | L | T | u | t | o | r | i | a | l |
Note: The position of the last character in the string is -1.
-12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
S | Q | L | T | u | t | o | r | i | a | l |
Note: The MID() and SUBSTR() functions equals the SUBSTRING() function. Continue reading MySQL MID Function
Remove leading spaces from a string:
SELECT LTRIM(" SQL Tutorial") AS LeftTrimmedString;
The LTRIM() function removes leading spaces from a string.
LTRIM(string)
Parameter | Description |
---|---|
string | Required. The string to remove leading spaces from |
Works in: | From MySQL 4.0 |
---|
Left-pad the string with “ABC”, to a total length of 20:
SELECT LPAD("SQL Tutorial", 20, "ABC");
The LPAD() function left-pads a string with another string, to a certain length.
Note: Also look at the RPAD() function.
LPAD(string, length, lpad_string)
Parameter | Description |
---|---|
string | Required. The original string. If the length of the original string is larger than the length parameter, this function removes the overfloating characters from string |
length | Required. The length of the string after it has been left-padded |
lpad_string | Required. The string to left-pad to string |
Works in: | From MySQL 4.0 |
---|
Left-pad the text in “CustomerName” with “ABC”, to a total length of 30:
SELECT LPAD(CustomerName, 30, "ABC") AS LeftPadCustomerName
FROM Customers;
Convert the text to lower-case:
SELECT LOWER(“SQL Tutorial is FUN!”);
The LOWER() function converts a string to lower-case.
Note: The LCASE() function is equal to the LOWER() function.
LOWER(text)
Parameter | Description |
---|---|
text | Required. The string to convert |
Works in: | From MySQL 4.0 |
---|
Convert the text in “CustomerName” to lower-case:
SELECT LOWER(CustomerName) AS LowercaseCustomerName
FROM Customers;
Search for “3” in string “Iampsp.com”, and return position:
SELECT LOCATE("3", "Iampsp.com") AS MatchPosition;
The LOCATE() function returns the position of the first occurrence of a substring in a string.
If the substring is not found within the original string, this function returns 0.
This function performs a case-insensitive search.
Note: This function is equal to the POSITION() function.
LOCATE(substring, string, start)
Parameter | Description |
---|---|
substring | Required. The substring to search for in string |
string | Required. The string that will be searched |
start | Optional. The starting position for the search. Position 1 is default |
Works in: | From MySQL 4.0 |
---|
Search for “com” in string “Iampsp.com” (start at position 3), and return position :
SELECT LOCATE("com", "Iampsp.com", 3) AS MatchPosition;
Search for “a” in CustomerName column, and return position:
SELECT LOCATE("a", CustomerName)
FROM Customers;
Return the length of the string, in bytes:
SELECT LENGTH("SQL Tutorial") AS LengthOfString;
The LENGTH() function returns the length of a string (in bytes).
LENGTH(string)
Parameter | Description |
---|---|
string | Required. The string to count the length for |
Works in: | From MySQL 4.0 |
---|
Return the length of the text in the “CustomerName” column, in bytes:
SELECT LENGTH(CustomerName) AS LengthOfName
FROM Customers;