Returns the starting position of the first occurrence of a specified pattern.
patindex("%pattern%", char_expr|uchar_expr[, using {bytes | characters | chars}])
select au_id, patindex("%circus%", copy) from blurbs
au_id ----------- ----------- 486-29-1786 0 648-92-1872 0 998-72-3567 38 899-46-2035 31 672-71-3249 0 409-56-7008 0
select name from sysobjects where patindex("sys[a-d]%", name) > 0
name ------------------------------ sysalternates sysattributes syscharsets syscolumns syscomments sysconfigures sysconstraints syscurconfigs sysdatabases sysdepends sysdevices
patindex, a string function, returns an integer representing the starting position of the first occurrence of pattern in the specified character expression, or a 0 if pattern is not found.
You can use patindex on all character data, including text and image data.
For text, unitext, and image data, if ciphertext is set to 1, then patindex is not supported. An error message appears.
For text, unitext, and image data, if ciphertext is set to 0, then the byte or character index of the pattern within the plaintext is returned.
select patindex("%C%", ut) from unitable ----------- 4
By default, patindex returns the offset in characters; to return the offset in bytes (multibyte character strings), specify using bytes.
Include percent signs before and after pattern. To look for pattern as the first characters in a column, omit the preceding %. To look for pattern as the last characters in a column, omit the trailing %.
If char_expr or uchar_expr is NULL, patindex returns 0.
If you give a varchar expression as one parameter and a unichar expression as the other, the varchar expression is implicitly converted to unichar (with possible truncation).
See also Transact-SQL Users Guide.
ANSI SQL – Compliance level: Transact-SQL extension.
Any user can execute patindex.