Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between mid and substring in mysql?

Tags:

mysql

i just saw both the MID() and SUBSTRING() function returns the same result. i want to know what is the difference between both?. Is both the function usage is same with different alias?

SELECT MID('mysample',2,3);

SELECT SUBSTRING('mysample',2,3); 

returns : ysa

like image 283
MaDHaN MinHo Avatar asked Dec 25 '22 10:12

MaDHaN MinHo


1 Answers

As the MySQL documentation says:

MID(str,pos,len) is a synonym for SUBSTRING(str,pos,len).

The documentation also says that MID() requires 3 arguments while SUBSTRING() is more flexible. The len argument is optional. When it is not present, the function returns the substring starting with pos until the end of the string.

SUBSTRING(str, pos, len)
SUBSTRING(str, pos)

SUBSTRING() also allows a more verbose syntax (inherited from the standard SQL):

SUBSTRING(str FROM pos FOR len)
SUBSTRING(str FROM pos)

Update:

Some tests I just ran on several different MySQL installations (different versions) reveal that on the current versions of MySQL, MID() is just a synonym of SUBSTRING() and it also accepts all the syntax variations of SUBSTRING().

like image 162
axiac Avatar answered Feb 15 '23 23:02

axiac