MySQL SUBSTR function

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); —>

Advertisements

Author: sqlandplsql

To learn more about the Oracle, SQL, PL SQL, Performance Tuning, Database Modeling, Ubuntu, MySQL etc .Suggestions, comments, feedbacks and referrals are highly appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s