Question: What is the difference between CHARINDEX vs PATINDEX?
Answer: We can use
either CHARINDEX or PATINDEX to search in a TEXT field in SQL SERVER.
The CHARINDEX and PATINDEX functions return the starting position of a
pattern you specify.
Both functions take two arguments. With
PATINDEX, you must include percent signs before and after the pattern,
unless you are looking for the pattern as the first (omit the first %)
or last (omit the last %) characters in a column. For CHARINDEX, the
pattern cannot include wildcard characters. The second argument is a
character expression, usually a column name, in which Adaptive Server
searches for the specified pattern.
Example of CHARINDEX:
USE AdventureWorks2014;
GO
SELECT CHARINDEX('important', DocumentSummary),
DocumentSummary
FROM Production.Document
WHERE Title = 'Repair and Service Guidelines';
GO
Examples of PATINDEX:
USE AdventureWorks2014;
GO
SELECT PATINDEX('%imp_rtant%', DocumentSummary),
DocumentSummary
FROM Production.Document
WHERE Title = 'Repair and Service Guidelines';
GO
Summary:
PATINDEX is CHARINDEX + WildCard Search. Use either of them is depending your business need.
PATINDEX is CHARINDEX + WildCard Search. Use either of them is depending your business need.
No comments:
Post a Comment