Position Function In Sql in SQL
POSITION Function in SQL
The POSITION function returns the starting position of a substring within a string. It returns the 1-based index 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 find.string— the string to search in.
Example:
Find the position of 'cat' in a string:
SELECT POSITION('cat' IN 'The black cat sat on the mat') AS position;Result:
| position |
|---|
| 11 |
Notes:
POSITIONis part of the SQL standard and supported by PostgreSQL, MySQL (since 8.0), Oracle (viaINSTR), and others.Similar to
CHARINDEXin SQL Server.Positions are 1-based (first character is position 1).
If you want, I can provide examples for specific databases or related functions!