SUBSTR function returns the sub string within a string.
Syntax:
SUBSTR(string, start_position, length)
or
SUBSTRING (string, start_position, length)
In MySQL both SUBSTR and SUBSTRING will work. SUBSTR is in ANSI standard.
Examples:-
mysql> select SUBSTR(‘sqlandplsql.com’,1,11);
will return – sqlandplsql
mysql> select SUBSTRING(‘sqlandplsql.com’,1,11);
will return – sqlandplsql
mysql> select SUBSTR(‘sqlandplsql.com’,2);
will return – qlandplsql
If you omit the length it will default to full string length.
mysql> select SUBSTR(‘sqlandplsql.com’,-3);
will return – com
Some more examples
select substr(‘sqlandplsql.com’,1,null); —> null
select substr(‘sqlandplsql.com’,null,null); —> null
select substr(‘sqlandplsql.com’,0,0); —>
select substr(‘sqlandplsql.com’,1,-2); —>
select substr(‘sqlandplsql.com’,0,3); —>