INF: Explanation of SQL Server PATINDEX Parameters and Output

Last reviewed: April 25, 1997
Article ID: Q69455

The information in this article applies to:

  - Microsoft SQL Server version 4.2 for OS/2

SUMMARY

The PATINDEX text/image function is defined as follows:

   Function    Parameters               Result
   --------    ----------               ------

   PATINDEX    ("pattern",column_name)  The starting position of
                                        the first occurrence of
                                        pattern in the specified
                                        column. You can use
                                        wildcard characters in
                                        pattern.

MORE INFORMATION

This means that PATINDEX will return the starting position of the string if it finds a match. If, however, a match is not found, the result is 0 (zero). The string pattern follows the standard rules regarding wildcards. The following SELECT PATINDEX returns 0 because there is no % wildcard to direct SQL Server to look for the pattern starting after the first byte:

   create table tab(col1 text)
   go
   insert tab values("testing")
   go
   select patindex("sting",col1) from tab
   go

Instead, you should use one of the following queries:

   select patindex("%sting",col1) from tab

   -or-

   select patindex("%sting%",col1) from tab

This second SELECT returns a value of 3.


Additional query words: Transact-SQL
Keywords : kbusage SSrvTran_SQL
Version : 4.2
Platform : OS/2


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 25, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.