MID() :
This function in MySQL is used to extract a substring from a given input string. If the starting position is a positive number, then the substring of the given length will be extracted from the starting index. If negative, then the substring of the given length will be extracted from the ending index.
Syntax :
MID(str,pos,len)
Parameters :
This function accepts 3 parameters.
- str –
A string from which we want to extract a substring.
- pos –
It indicates the position in the input string from where extraction will be started.
- len –
It indicates the length of the string which we want to extract.
Returns :
It extracts a substring from a given input string.
Example-1 :
Extracting a string of length 5 from the string ‘geeksforgeeks’ with the help of MID Function starting from pos 1.
SELECT MID('geeksforgeeks', 1, 5) As SUBSTRING;
Output :
Example-2 :
Extracting a string of length 3 from the string ‘Learning MySQL is fun’ with the help of MID Function starting from pos -3.
SELECT MID('Learning MySQL is fun', -3, 3) As SUBSTRING;
Output :
Example-3 :
MID Function can also be used on column data.
Creating a Student table –
CREATE TABLE StudentDetails
(
Student_id INT AUTO_INCREMENT,
Student_name VARCHAR(100) NOT NULL,
Roll INT NOT NULL,
Department VARCHAR(10) NOT NULL,
PRIMARY KEY(Student_id )
);
Inserting values into the table –
INSERT INTO StudentDetails
(Student_name ,Roll, Department )
VALUES
('Anik Biswas ',10100,'CSE'),
('Bina Mallick', 11000,'ECE' ),
('Niket Sharma', 12000,'IT' ),
('Sayan Samanta',13000, 'ME' ),
('Riya Shah ', 14000,'EE' ),
('Bipin Kohli', 15000,'CE' );
The table will look as follows.
SELECT * from StudentDetails;
STUDENT_ID |
STUDENT_NAME |
ROLL |
DEPARTMENT |
1 |
Anik Biswas |
10100 |
CSE |
2 |
Bina Mallick |
11000 |
ECE |
3 |
Niket Sharma |
12000 |
IT |
4 |
Sayan Samanta |
13000 |
ME |
5 |
Riya Shah |
14000 |
EE |
6 |
Bipin Kohli |
15000 |
CE |
Now, we are going to use MID Function on the Student_name column to find the first name of every student.
SELECT Student_id , MID(Student_name,1,5 ) AS First_Name,
Student_name ,Roll,Department FROM StudentDetails;
Output :
STUDENT_ID |
FIRST_NAME |
STUDENT_NAME |
ROLL |
DEPARTMENT |
1 |
Anik |
Anik Biswas |
10100 |
CSE |
2 |
Bina |
Bina Mallick |
11000 |
ECE |
3 |
Niket |
Niket Sharma |
12000 |
IT |
4 |
Sayan |
Sayan Samanta |
13000 |
ME |
5 |
Riya |
Riya Shah |
14000 |
EE |
6 |
Bipin |
Bipin Kohli |
15000 |
CE |
Similar Reads
MOD() Function in MySQL
The MOD() function in MySQL is used to find the remainder of one number divided by another. The MOD() function returns the remainder of dividend divided by divisor. if the divisor is zero, it returns NULL. Syntax: MOD(N, M) or N % M or N MOD M Parameter : MOD() function accepts two parameter as ment
4 min read
MINUTE() Function in MySQL
MINUTE() function : This function in MySQL is used to return the minutes part for a specified time or date time value. The range of minute value is from 0 to 59. For example, if the specified time is "09:12:23", this function will return 12 minute. Syntax : MINUTE(datetime) Parameter : This method a
1 min read
LPAD() Function in MySQL
LPAD() function in MySQL is used to pad or add a string to the left side of the original string. Syntax : LPAD(str, len, padstr) Parameter : This function accepts three parameter as mentioned above and described below - str - The actual string which is to be padded. If the length of the original str
2 min read
MySQL | MD5 Function
The MySQL MD5 function is used to return an MD5 128-bit checksum representation of a string. The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value. The value returned by the MD5 function is a binary string of 32 hexadecimal digits, or NULL if the argument was
1 min read
INSTR() function in MySQL
INSTR() : This function in MySQL is used to return the location of the first occurrence of a substring within a given string. Syntax : INSTR(string_1, string_2) Parameters : This function accepts 2 parameters. string_1 - The string where searching takes place. string_2 - The string/sub-string which
2 min read
LOG() Function in MySQL
LOG() function in MySQL is used to calculate the natural logarithm of a specific number. The number must be >0 Otherwise it will return NULL. Syntax : LOG(X) Parameter : This method accepts one parameter as mentioned above and described below : X : A number whose logarithm value we want to calcul
3 min read
LTRIM() Function in MySQL
LTRIM() : This function in MySQL is used to remove leading spaces from a string. Syntax : LTRIM(str) Parameter : It accepts one parameter as mentioned above and described below as follows. str â The string from which we want to remove leading spaces. Returns : It returns a string after truncating al
2 min read
MAKETIME() Function in MySQL
MAKETIME() function : This function in MySQL is used to create and return a time value based on specified hour, minute, and second value. In MySQL's TIME value limit is 838:59:59. Syntax : MAKETIME(hour, minute, second) Parameters : This method accepts three parameters as given below - hour : Specif
1 min read
MAKEDATE() function in MySQL
MAKEDATE() : This function in MySQL is used to create and return a date based on a year and a number of days value. The number of days must be greater than 0 otherwise it returns a NULL value. Syntax : MAKEDATE(year, day) Parameter : This function accepts two parameters as given below as follows. ye
2 min read
LEFT() Function in MySQL
The LEFT() function in MySQL is used to extract a specified number of characters from the left side of a given string. It uses its second argument to decide, how many characters it should return. Syntax: LEFT (str, len)Parameter: This function accepts two parameters as mentioned above and described
1 min read