mySql  substring function

the mysql substring function is used to extract characters from a text field.

mysql substring() syntax

         select    substring(string, pos,len)    from   table_name;

the mid(str,pos,len) is a synonym for substring(str,pos,len).

mysql mid() syntax

         select    mid(string, pos,len)    from   table_name;
parameterdescription
stringrequired. the field to extract characters from
posrequired. specifies the starting position(starts at 1)
lenoptional.the number of characters to return.

The demo database table use in the below MySql substring function example

below is a selection from the "users" table:

useridfirstnamelastnamesalarybonus
1sandeepkumar10,0001000
2ankitkumar20,0002000
3ramanehra30,0003000
4vikashnehra12,0002000

mysql substring function example

               
    select    substring(firstname,1,4)   as  sub_name    from    users;
               
    select    mid(firstname,1,4)   as  sub_name    from    users;

output :

sub_name
sand
anki
ram
vika