What's the difference between CHARINDEX and PATINDEX?

Posted on 2012-08-10
Last Modified: 2012-08-10
Hi All

Question for you:  What's the difference between CHARINDEX and PATINDEX?
The difference is not clear to me.

When I run the below script it returns the same values:

Declare @str varchar(100) = 'abcdefghigklmnopqrstuvwxyz_e'

Searches an expression for another expression and returns its starting position if found. 

-- Select the first e in a string

-- Select the first e in a string, starting at the 10th character
SELECT CHARINDEX('e', @str, 10)


Turns the starting position of the first occurrence of a pattern in a specified expression, 
  or zeros if the pattern is not found, on all valid text and character data types. 
SELECT PATINDEX('%efg%', @str)

Open in new window

They both return bigint's that are the starting values.

Full disclosure:  I'm studying for the 70-433 exam, and this is not a work-related question.
Question by:Jim Horn
    LVL 29

    Assisted Solution

    by:Paul Jackson
    LVL 7

    Assisted Solution

    The PATINDEX function allows for using wildcard syntax in the pattern being searched for, where as the CHARINDEX function does not support wildcard character searches. Let's look at each one of these functions in a little more detail.

    A good page to look at is here:
    Differences between CharIndex and PatIndex and how to use them.
    LVL 92

    Accepted Solution

    More generally, PATINDEX searches for a pattern, while CHARINDEX searches for a substring.

    So, it's not just wild cards.  For example, you can use PATINDEX to find the starting position for the first substring that consists of three consecutive digits:

    PATINDEX('%[0-9][0-9][0-9]%', SomeColumn)

    Sort of like "Regular Expressions Lite" :)
    LVL 65

    Author Closing Comment

    by:Jim Horn
    >PATINDEX can use wildcard characters, but CHARINDEX cannot.
    Okay.  That I saw, but was wondering if it was any other difference.  Now I know.   Thanks.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now