Locate the position of “3” within the string “W3Schools.com” and return its index.
SELECT POSITION(“3” IN “W3Schools.com”) AS MatchPosition; |
The POSITION() function provides the position of the first occurrence of a substring in a string.
If the substring isn’t found within the original string, this function returns 0.
This function conducts a case-insensitive search.
Note: that the LOCATE() function is equivalent to the POSITION() function.
POSITION(substring IN string) |
Parameter |
Description |
substring |
Input required: The substring to be searched for within the string. |
string |
Input required: The original string in which the search will be performed. |
Works in: | From MySQL 4.0 |
Look for “COM” within the string “W3Schools.com” and provide its position.
SELECT POSITION(“COM” IN “W3Schools.com”) AS MatchPosition; |
Find the position of “a” within the CustomerName column and return its index.
SELECT POSITION(“a” IN CustomerName) FROM Customers; |