SQL MID() and SQL-Server SUBSTRING() FUNCTION



SQL MID() and SQL-Server SUBSTRING() Function

MID() – The MID() function is used to return exact text from given text field.
The syntax of MID() for SQL :

SELECT MID(col_name, strat, length) as some col_name FROM Table_Name

 start - starting position of text
 length - length or number of character to return.

 

In SQL Server we use SUBSTRING() function.
The syntax of SUBSTRING() in SQL Server :

SELECT SUBSTRING(col_name, strat, length) as some col_name FROM Table_Name
sql mid and substring
Sql MID() and SQL server SUBSTRING() function

Here, we understand MID() and SUBSTRING() function with an example.
We have SQL Table StudentMst with below column.

SELECT * FROM StudentMst

ID Name City Pincode Mobile
1 Meera Bombay 380022 7874555555
2 Rahul Surat 352200 7874444444
3 Jayesh Ahmedabad 352200 7874333333
4 Dhvanish Bombay 380022 7874111111
5 Reena Baroda 352222 7874666666
6 Veera Baroda 352222 7874121212

 

SELECT MID(name, 1, 3) as  Name, MID(city, 1, 3) as  City FROM StudentMst

Name City
Mee omb
Rah ura
Jay hme
Dhv omb
Ree aro
Vee aro

 

SELECT SUBSTRING(name, 1, 3) as  Name, SUBSTRING(city, 1, 3) as  City FROM StudentMst

Name City
Mee omb
Rah ura
Jay hme
Dhv omb
Ree aro
Vee aro

Leave a Reply

Your email address will not be published. Required fields are marked *