Friday, December 20, 2024

MySQL POSITION() Function

 The POSITION() function in MySQL is used to find the position of a substring within a string. It returns the index (1-based) of the first occurrence of the substring. If the substring is not found, it returns 0.

Syntax:

POSITION(substring IN string)
  • substring: The substring you want to search for.
  • string: The string in which to search for the substring.

Example:

SELECT POSITION('world' IN 'Hello world');

This will return 7 because the substring 'world' starts at the 7th position in the string 'Hello world'.

Notes:

  • The POSITION() function is case-sensitive.
  • The returned position starts at 1, not 0.

Example with no match:

SELECT POSITION('abc' IN 'Hello world');

This will return 0 because the substring 'abc' is not found in the string 'Hello world'.

No comments:

Post a Comment