Example
Compare two strings:
SELECT STRCMP("SQL Tutorial", "SQL Tutorial");
Definition and Usage
The STRCMP() function compares two strings. Continue reading MySQL STRCMP Function
Compare two strings:
SELECT STRCMP("SQL Tutorial", "SQL Tutorial");
The STRCMP() function compares two strings. Continue reading MySQL STRCMP Function
Return a string with 10 space characters:
SELECT SPACE(10);
The SPACE() function returns a string of the specified number of space characters. Continue reading MySQL SPACE Function
Remove trailing spaces from a string:
SELECT RTRIM("SQL Tutorial ") AS RightTrimmedString;
The RTRIM() function removes trailing spaces from a string. Continue reading MySQL RTRIM Function
Right-pad the string with “ABC”, to a total length of 20:
SELECT RPAD("SQL Tutorial", 20, "ABC");
The RPAD() function right-pads a string with another string, to a certain length.
Note: Also look at the LPAD() function. Continue reading MySQL RPAD Function
Extract 4 characters from a string (starting from right):
SELECT RIGHT("SQL Tutorial is cool", 4) AS ExtractString;
The RIGHT() function extracts a number of characters from a string (starting from right).
Tip: Also look at the LEFT() function. Continue reading MySQL RIGHT 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