Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 01 Sep, 2021

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 : 
 

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

MID(string, start_position, length)

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

  • string – 
    The string on which the MID() Function is applied.
  • start_position – 
    The starting position of the string to be extracted.
  • length – 
    The length of the string to be extracted.

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 : 
 

  • substring : The string to be searched for.
  • string : The string in which the search operation is done.

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

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!