This MySQL function returns index of the character in string where substring first appears.


Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

POSITION(substring IN string)


This function returns index of the character in string where substring first appears. The first character of string is numbered 1. This function is like LOCATE( ), except that the keyword IN is used instead of a comma to separate the substring and containing string. Also, this function does not provide a starting point to begin the search, it must begin from the left-most character.


UPDATE courses
SET course_name =
INSERT(course_name, POSITION('Eng.' IN course_name), 4, 'English')
WHERE course_name LIKE '%Eng.%';

In this example, it was discovered that some course names have the word English abbreviated as Eng. This SQL statement overwrites any such occurrences with the word English. It uses the POSITION() function to find the starting point of the abbreviation. The numerical value it returns is then used as the position argument for the INSERT() function (not the INSERT statement). If it's not found, the course name will not be changed because a value of 0 will be returned by POSITION(), and the INSERT() function ignores any request in which position lies outside the length of the original string.