SUBSTR(), This function returns a substring starting from the specified position.
Both MID() and SUBSTR() are synonyms of SUBSTRING().
The forms without a length argument return a substring from a string starting at a specified position. See illustrations 1 and 2.
The forms with a length argument return a substring length characters long from the string, starting at a specified position. See illustrations 3 and 5.
The forms that use FROM are standard SQL syntax.
It is also possible to use a negative(-ve) value for the position. In this case, the beginning of the substring is position characters from the end of the string, rather than the beginning.
A negative(-ve) value may be used for the position in any of the forms of this function. See illustrations 6 and with every illustration, you can see how negative the position value will work.
A value of 0 for position returns an empty string. See illustrations 7.
If length < 1, the result is the empty string. See illustrations 8.
For all forms of substring(), the position of the first character in the string from which the substring is to be extracted is to take into account as 1.
Below are several ways on how we can use this function, so the all possible ways of syntax’s looks like this:
SUBSTR(string , position );
SUBSTR(string FROM position );
SUBSTR ( string , position, length);
SUBSTR(string FROM position FOR length);
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description
string , Required, String , The input string.
position, Required, Numeric, The position from where the substring starts.
length, Optional, Numeric , The length tells the number of characters to return from that starting position.
[/table]
[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Returns,
String, returns a substring starting from the specified position.
[/table]
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 4.0
[/table]
Below is an example with SUBSTR(string, position):
mysql> SELECT SUBSTR('database mysql', 5) as Output; +------------+ | Output | +------------+ | base mysql | +------------+ 1 row in set (0.00 sec)
mmysql> SELECT SUBSTR('database mysql', -5 ) as Output; +--------+ | Output | +--------+ | mysql | +--------+ 1 row in set (0.07 sec)
Below is an example :
mysql> SELECT SUBSTR('database mysql' FROM 5) as Output; +------------+ | Output | +------------+ | base mysql | +------------+ 1 row in set (0.00 sec)
mysql> SELECT SUBSTR('database mysql' FROM -5 ) as Output; +--------+ | Output | +--------+ | mysql | +--------+ 1 row in set (0.00 sec)
Below is an example of it.
mysql> SELECT SUBSTR('database mysql', 5, 4) as Output; +--------+ | Output | +--------+ | base | +--------+ 1 row in set (0.00 sec)
mysql> SELECT SUBSTR('database mysql', -10, 3) as Output; +--------+ | Output | +--------+ | bas | +--------+ 1 row in set (0.00 sec)
It will return NULL if any of the arguments is NULL. Let’s see the below example.
mysql> SELECT SUBSTR('database mysql' ,NULL, 4 ) as Output1, SUBSTR('database mysql',5 ,NULL ) as Output2, SUBSTR(NULL, 1, 14 ) as Output3; +---------+---------+------------------+ | Output1 | Output2 | Output3 | +---------+---------+------------------+ | NULL | NULL | 0x | +---------+---------+------------------+ 1 row in set (0.00 sec)
Let’s see the below example.
mysql> SELECT SUBSTR('database mysql' FROM 5 FOR 4) as Output; +--------+ | Output | +--------+ | base | +--------+ 1 row in set (0.00 sec)
mysql> SELECT SUBSTR('database mysql' FROM -10 FOR 4) as Output; +--------+ | Output | +--------+ | base | +--------+ 1 row in set (0.00 sec)
Let’s see the below example.
mysql> SELECT SUBSTR('database mysql',-5 ) as Output; +--------+ | Output | +--------+ | mysql | +--------+ 1 row in set (0.00 sec)
Let’s see the below example.
mysql> SELECT SUBSTR('database mysql' FROM 0 FOR 4) as Output; +--------+ | Output | +--------+ | | +--------+ 1 row in set (0.00 sec)
Let’s see the below example.
mysql> SELECT SUBSTR('database mysql', 10, -5) as Output; +--------+ | Output | +--------+ | | +--------+ 1 row in set (0.00 sec)
See all MySQL String functions MySQL 8 String Functions.
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…