In function INSERT(str, Pos, len, newstr), what would be the result if ‘Pos’ is not within the length of the string?


MySQL INSERT() function performs no insertion if the position of insertion is not within the length of the string. There are certain cases like we pass a negative or 0(zero) value or the value goes beyond the value of a total number of characters in an original string by 2 when we can say that ‘pos’ is not within the length of the string. It can be understood with the help of the following example −

Example

The query below will perform no insertion because the ‘pos’ is not within the length of string i.e. a negative value.

mysql> Select INSERT('Tutorialspoint',-1,4,'.com');
+--------------------------------------+
| INSERT('Tutorialspoint',-1,4,'.com') |
+--------------------------------------+
| Tutorialspoint                       |
+--------------------------------------+
1 row in set (0.00 sec)

The query below will perform no insertion because the ‘pos’ is not within the length of string i.e. 0 (zero).

mysql> Select INSERT('Tutorialspoint',0,4,'.com');
+-------------------------------------+
| INSERT('Tutorialspoint',0,4,'.com') |
+-------------------------------------+
| Tutorialspoint                      |
+-------------------------------------+
1 row in set (0.00 sec)

The query below will perform no insertion because the ‘pos’ is not within the length of string i.e. goes beyond the value of a number of characters in an original string by 2. In the example below, the original string ‘Tutorialspoint’ is having 14 characters and the value of position we give is 16 hence no insertion happens.

mysql> Select INSERT('Tutorialspoint',16,4,'.com');
+--------------------------------------+
| INSERT('Tutorialspoint',16,4,'.com') |
+--------------------------------------+
| Tutorialspoint                       |
+--------------------------------------+
1 row in set (0.00 sec)

Updated on: 22-Jun-2020

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements