Tags
MySQL , MID
Asked 2 years ago
31 May 2022
Views 560
kiran

kiran posted

what is the use of MID in MySQL?

what is MID in MySQL ? how to use function MID in MySQL?
noob

noob
answered Jun 5 '22 00:00

MID() function is used to extract part of string until given length in MySQL.
MID() function is used to extract part of string from given starting point until given length in MySQL.

MID() syntax :

 MID(string ,starting_position ,until_length )  

string is the string that get processed in this function
starting_position is the number, non zero which is the starting point from of new returned string from MID.
until_length is the number, is the length of the returned string from MID.

so if
MID("Data",2,2);
so above MID() function will return "at" (start from 2 to 2 length)

what if we provide zero number to the MID() second argument ?
If we provide zero as MID() second argument than it will return empty string or FALSE MID("Data",0,2);
above query return empty string , zero or False because second argument of MID() function is zero.
select MID("Data",0,2)=FALSE; //1
select MID("Data",0,2)=0; //1
select MID("Data",0,2)=''; //1

what if we provide a negative number to the MID() second argument?
a negative number means it starts with the end of the string, so if you provide -1 means it starts with the last character of the string,
and the third argument positive means it goes toward the end of the string. so means MID("Data",-2,2); start with the
second last character and go to the next two characters so it will return "ta" from the "Data" word.

select MID("Data",-1,2);//a
select MID("Data",-2,2);//ta
select MID("Data",-3,2);//at
select MID("Data",-4,2);//da



select MID("Data",-4,-2);//return empty


what if we provide a negative number to the MID() third argument?
In any case, if you provide a negative number to the third argument to the MID() function. then it always returns empty, false, or zero

select MID("Data",-4,-2);//return empty


what if we provide zero number to the MID() third argument?
In any case, if you provide a zero number to the third argument to the MID() function. then it always returns empty, false, or zero

select MID("Data",-4,0);//return empty
Post Answer