Open In App

MID(), POSITION() and LENGTH() Function in MariaDB

1. MID() Function : 
In MariaDB, the MID() Function is used to extract a substring from a string. It will return a substring of a given length from the starting point. If the starting position is a positive number, then it will start from the starting index. If negative, then it will start from the ending index. 

Syntax : 
 



MID(string, start_position, length)

Parameters : 
This function accepts three parameters as mentioned above and described below : 
 

Returns : The substring of given length from the starting point. 



Example-1 : 
 

SELECT MID('geeksforgeeks', 1, 4);

Output : 
 

geek

Example-2 : 
 

SELECT MID('computerscience', 2, 2);

Output : 
 

om

Example-3 : 
 

SELECT MID('algorithm', -3, 3);

Output : 
 

him

2. POSITION() Function : 
In MariaDB, the POSITION() Function is used for finding the location of a substring in a string. It will return the location of the first occurrence of the substring in the string. If the substring is not present in the string then it will return 0. It works similar to the LOCATE() Function. When searching for the location of a substring in a string then the function does not perform a case-sensitive search. 

Syntax : 
 

POSITION(substring IN string)

Parameters : This function accepts two parameters as mentioned above and described below : 
 

Returns : The location of the first occurrence of the substring in the string. 

Example-1 : 
 

SELECT POSITION('g' IN 'gfg');

Output : 
 

1

Example-2 : 
 

SELECT POSITION('s' IN 'DSASELFPACED');

Output : 
 

2

Example-3 : 
 

SELECT POSITION('X' IN 'geeksforgeeks');

Output : 
 

0

3. LENGTH() Function : 
In MariaDB, the LENGTH() Function is used for the returns the length of the specified string. It will be measured in bytes. It works similar to the CHAR_LENGTH function. 

Syntax : 
 

LENGTH(string)

Parameter : Required. A string value. 
Returns : The length of the input string. 

Example-1 : 
 

SELECT LENGTH('geeksforgeeks');

Output : 
 

13

Example-2 : 
 

SELECT LENGTH('');

Output : 
 

0

Example-3 : 
 

SELECT LENGTH(' ');

Output : 
 

1

 

Article Tags :
SQL