Javatpoint Logo
Javatpoint Logo

MID Function in SQL

The MID string function in the Structured Query Language shows the characters or sub-string from the specific index value of the original string. SQL also allows you to use the MID function with the tables. The MID function is similar to the SUBSTR string function.

Syntax of MID String Function

Syntax1: This syntax uses the MID function with the column name of the SQL table:

In this syntax, we have to define the name of that column on which we want to execute the MID() function. Here, the Number_of_Characters argument is optional. If it is omitted, then this function extracts the whole string from the starting index value.

Syntax2: This syntax uses the MID function with the string:

Syntax2: This syntax uses the MID function with a single character:

Examples of MID String function

Example 1: The following SELECT query shows the characters from the 17th position of the given string.

This SQL query returns the 24 characters with spaces after the 17th position in the string.

Output:

website for professionals

Example 2: The following SELECT query shows the characters from the -17th position of the given string:

This SQL query shows the five characters from the last 17th position of the string.

Output:

website for professionals

Example 3: The following SELECT query shows all the characters from the 5th position of the string.

Output:

Delhi IS the Capital OF India

Example 4: The following SELECT query shows the single character from the 8th position of the string.

Output:

n

Example 5: This example uses the MID function with the SQL table

In this example, we are going to create a new table on which we want to execute the MID function.

The syntax for creating the new SQL table is mentioned in the below block:

The following CREATE statement creates the Schoolboy_Marks table:

The below INSERT queries insert the records of college Faculties in the Schoolboy_Marks table:

The following SELECT statement displays the inserted records of the above Schoolboy_Marks table:


Schoolboy_Id Schoolboy_First_Name Schoolboy_Middle_Name Schoolboy_Last_Name Schoolboy_Class Schoolboy_City Schoolboy_State Schoolboy_Marks
4001 Aman Roy Sharma 4 Chandigarh Punjab 88
4002 Vishal Gurr Sharma 8 Murthal Haryana 95
4007 Raj singhania Gupta 6 Ghaziabad Uttar Pradesh 91
4004 Yash Chopra Singhania 9 Jaipur Rajasthan 85
4011 Vinay Sharma Roy 8 Chandigarh Punjab 94
4006 Manoj Singhania Gupta 5 Ghaziabad Uttar Pradesh 83
4010 Ram Raheem Gupta 9 Lucknow Uttar Pradesh 89

Query 1: The following SELECT query uses the MID function with the Schoolboy_Last_Name column of the above Schoolboy_Marks table:

This SQL statement shows the four characters after the 2nd position of the Last name of each student.

Output:

Schoolboy_Last_Name MID_2_4
Sharma harm
Sharma harm
Gupta upta
Singhania ingh
Roy oy
Gupta upta
Gupta upta

Query 2:The following SELECT query uses the MID function with the Schoolboy_Last_Name column of the above Schoolboy_Marks table: