Asked 2 years ago
31 May 2022
Views 716
QuickIos

QuickIos posted

what is the use of SUBSTR in MySQL?

what is SUBSTR in MySQL ? how to use function SUBSTR in MySQL?
steave

steave
answered May 31 '22 00:00

The SUBSTR() function returns part of the given string based on the given starting point to a certain length in MySQL.
syntax :
SUBSTR(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 SUBSTR returns part of the string.

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

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


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


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

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

select SUBSTR("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 SUBSTR("MYSQL",1,2);// return  is MY
select SUBSTR("MYSQL",1);// return  is MYSQL 
sqltreat

sqltreat
answered Jun 4 '22 00:00

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

select SUBSTR("MYSQL", 3);
select SUBSTRING("MYSQL", 3);

so above both query returns same output is "SQL"
Post Answer