Friday 7 October 2016

Difference between PATINDEX and CHARINDEX in SQL SERVER 2008R2?



CHARINDEX :-  It is used to find a start_position of a specified text in the expression. It accepts two parameters.

Syantax:-

Select CHARINDEX(string_expression,target_expression)

Example:- 

Select CHARINDEX('e','abcdeavb')

In the above we try to find postion of ‘e’ in ‘abcdeavb’ expression.

PATINDEX:- It is also used to find start_position of specified text in the expression. It accepts two parameters. In PATINDEX we can use wildcard character like ‘%’ and ‘_’ . 

Syntax:-

Select PATINDEX(search_pattern_literal,expression)

Example:-
Select PATINDEX('%ab%','sadabdfkjafbksdfk')

Output:-
4

Select PATINDEX('%a_b%','sadabdfkjafbksdfk')

Output:-
10

No comments:

Post a Comment

Note: only a member of this blog may post a comment.