Collecting rest of the string from 4th posting ( from left )
SELECT SUBSTRING('plus2net.com',4); // Output is s2net.com
Last 4 chars ( from right end)
SELECT SUBSTRING('plus2net.com',-4); // Output is is .com
With optional parameter length
Let us add number of chars we will get by adding length
Starting from 3rd position 5 chars to be collected
SELECT SUBSTRING('plus2net.com',3,5); // Oputput us2ne
Starting from end, from 6th position 2 ( length) chars to be collected
select SUBSTRING('plus2net.com',-6,2); // Output is et
Using FROM to get part of string
Rest of the string from a position
SELECT SUBSTRING('plus2net.com' FROM 3) // Ouptut is us2net.com
From right ( end ) starting from 7 position return 3 chars (length)
SELECT SUBSTRING('plus2net.com' FROM -7 FOR 3) // Output is net
Removing Last one char
select substring('plus2net.com',1,char_length('plus2net.com')-1)
Use this code in our student table.
SELECT id,SUBSTRING(class,1,char_length(class)-1) as class FROM student
using concat()
SELECT CONCAT(SUBSTRING('Hello, world!', 1, 5), SUBSTRING('Hello, world!', -6)); -- Output: 'Helloworld!'
Substring and substring_index
Substring returns part of string by taking position ( number ) as input. If the position is not known and to use a specific delimiter for searching, then substring_index function can be used.
How substring is used to collect domain part of Email address
← SQL String References
substring_index to get part of string using delimiter→
← Subscribe to our YouTube Channel here