Asked 2 years ago
31 May 2022
Views 706
jagdish

jagdish posted

how to use function SUBSTRING in MySQL?

how to use SUBSTRING in MySQL ? can you please explain function SUBSTRING in MySQL with some example?
jqueryLearner

jqueryLearner
answered Jun 4 '22 00:00

The SUBSTRING() function returns part of the given string based on the given starting point to a certain length in MySQL.
syntax :
SUBSTRING(string, start,length)
the first argument is string field or string value
start is the starting point of the string from where
length defines how many characters need to return or extract, length is optional

the function SUBSTRING returns part of the string.

how does the SUBSTRING function work in MySQL?
let us get by example how the SUBSTRING function works?
select SUBSTRING("MYSQL", 3,3) so SUBSTRING function return "SQL" from "MYSQL" because it had second argument 3 which is say SUBSTRING method to start to return part of after of 3 characters of the given string.
examples :

select SUBSTRING("MYSQL", 3)
// return  is SQL


select SUBSTRING("MYSQL", -4)
// return  is YSQL


select SUBSTRING("MYSQL", 0)
// return  is NULL OR EMPTY 

if you give second argument more than main string length than it returns null or empty by SUBSTRING() function in MySQL.

select SUBSTRING("MYSQL",10)
// return  is NULL OR EMPTY 


if you don't provide the last argument length then it takes as main string full length.

select SUBSTRING("MYSQL",1,2);// return  is MY
select SUBSTRING("MYSQL",1);// return  is MYSQL 
shyam

shyam
answered Jun 4 '22 00:00

SUBSTR() function and SUBSTRING() function both are same in MySQL.

select SUBSTR("MYSQL", 3);
select SUBSTRING("MYSQL", 3.3);
Post Answer