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
As the MySQL documentation
says:
MID(str,pos,len)
is a synonym forSUBSTRING(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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With