Link to home
Start Free TrialLog in
Avatar of Jim Horn
Jim HornFlag for United States of America

asked on

What's the difference between CHARINDEX and PATINDEX?

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'

/*
CHARINDEX
http://msdn.microsoft.com/en-us/library/ms186323.aspx
Searches an expression for another expression and returns its starting position if found. 
*/

-- Select the first e in a string
SELECT CHARINDEX('ef', @str)

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

/*
PATINDEX
http://msdn.microsoft.com/en-us/library/ms188395

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.
SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim Horn

ASKER

>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.