MySQL STRCMP Function

Example

Compare two strings:

SELECT STRCMP("SQL Tutorial", "SQL Tutorial");

Definition and Usage

The STRCMP() function compares two strings. Continue reading MySQL STRCMP Function

MySQL RTRIM Function

Example

Remove trailing spaces from a string:

SELECT RTRIM("SQL Tutorial     ") AS RightTrimmedString;

Definition and Usage

The RTRIM() function removes trailing spaces from a string. Continue reading MySQL RTRIM Function

MySQL RPAD Function

Example

Right-pad the string with “ABC”, to a total length of 20:

SELECT RPAD("SQL Tutorial", 20, "ABC");

Definition and Usage

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

MySQL RIGHT Function

Example

Extract 4 characters from a string (starting from right):

SELECT RIGHT("SQL Tutorial is cool", 4) AS ExtractString;

Definition and Usage

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

MySQL REPLACE Function

Example

Replace “SQL” with “HTML”:

SELECT REPLACE("SQL Tutorial", "SQL", "HTML");

Definition and Usage

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

MySQL REPEAT Function

Example

Repeat a string 3 times:

SELECT REPEAT("SQL Tutorial", 3);

Definition and Usage

The REPEAT() function repeats a string as many times as specified. Continue reading MySQL REPEAT Function

MySQL POSITION Function

Example

Search for “3” in string “Iampsp.com”, and return position:

SELECT POSITION(“3” IN “Iampsp.com”) AS MatchPosition;

Definition and Usage

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

MySQL MID Function

Example

Extract a substring from a string (start at position 5, extract 3 characters):

SELECT MID("SQL Tutorial", 5, 3) AS ExtractString;

Definition and Usage

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