MySQL - String Library Function

CONCAT: This function concatenates two or more strings together.

Example:

SELECT CONCAT('Hello', ' ', 'World');

Output:

Hello World

 

CONCAT_WS: This function concatenates two or more strings together with a separator.

Example:

SELECT CONCAT_WS('-', '2022', '03', '09');

Output:

2022-03-09

 

FIND_IN_SET: This function searches for a value within a comma-separated list.

Example:

SELECT FIND_IN_SET('apple', 'banana,apple,orange');

Output:

2

 

FORMAT: This function formats a number with a specified number of decimal places.

Example:

SELECT FORMAT(123456789.12345, 2);

Output:

123,456,789.12

 

LCASE: This function converts a string to lowercase.

Example:

SELECT LCASE('Hello World');

Output:

hello world

 

LENGTH: This function returns the length of a string.

Example:

SELECT LENGTH('Hello World');

Output:

11

 

MID: This function returns a specified portion of a string.

Example:

SELECT MID('Hello World', 1, 5);

Output:

Hello

 

POSITION: This function returns the position of a substring within a string.

Example:

SELECT POSITION('World' IN 'Hello World');

Output:

7

 

REPEAT: This function repeats a string a specified number of times.

Example:

SELECT REPEAT('a', 5);

Output:

aaaaa

 

REPLACE: This function replaces a substring within a string with another substring.

Example:

SELECT REPLACE('Hello World', 'World', 'Universe');

Output:

Hello Universe

 

STRCMP: This function compares two strings and returns 0 if they are equal, -1 if the first string is less than the second, and 1 if the first string is greater than the second.

Example:

SELECT STRCMP('Hello', 'World');

Output:

-1

 

UCASE: This function converts a string to uppercase.

Example:

SELECT UCASE('Hello World');

Output:

HELLO WORLD

 

SUBSTR: This function returns a specified portion of a string.

Example:

SELECT SUBSTR('Hello World', 1, 5);

Output:

Hello