PATINDEX SQL function
1. Usage of PATINDEX to find 'restaurant' in company name.
SQL Server Query 1
SELECT
CustomerID,
CompanyName,
PATINDEX('%Restaurant%', CompanyName) AS RestaurantPosition
FROM
Customers
WHERE
PATINDEX('%Restaurant%', CompanyName) > 0;
Create SQL query with SqlQueryBuilder 1
var (sql1, parameters1) = new SqlQueryBuilder()
.Select()
.Columns("CustomerID", "CompanyName")
.Column(new PATINDEX("%Restaurant%", new Column("CompanyName")), "RestaurantPosition")
.From("Customers")
.Where(new Where(new PATINDEX(new CONCAT("%", "Restaurant", "%"), new Column("CompanyName")), SQLComparisonOperators.GREATER_THAN, 0))
.Build();
Query build by SqlQueryBuilder 1
SELECT CustomerID,
CompanyName,
PATINDEX(@pMAIN_2507200132162282330, CompanyName) AS RestaurantPosition
FROM Customers
WHERE PATINDEX(CONCAT(@pMAIN_2507200132162282331, @pMAIN_2507200132162282332, @pMAIN_2507200132162282333), CompanyName) > @pMAIN_2507200132162282334;
Parameters (If used)
Name |
Value |
@pMAIN_2507200132162282330 |
%Restaurant% |
@pMAIN_2507200132162282331 |
% |
@pMAIN_2507200132162282332 |
Restaurant |
@pMAIN_2507200132162282333 |
% |
@pMAIN_2507200132162282334 |
0 |
Query Results 1:
|
CustomerID |
CompanyName |
RestaurantPosition |
1 |
GROSR
|
GROSELLA-Restaurante
|
10
|
2 |
LONEP
|
Lonesome Pine Restaurant
|
15
|
3 |
TORTU
|
Tortuga Restaurante
|
9
|
2. Usage of PATINDEX to find position of 'chef' keyword.
SQL Server Query 2
SELECT
ProductID,
ProductName,
CASE
WHEN PATINDEX('%Chef [A-Za-z]%', ProductName) > 0
THEN PATINDEX('%Chef [A-Za-z]%', ProductName)
WHEN PATINDEX('%[0-9][gG]%', ProductName) > 0
THEN PATINDEX('%[0-9][gG]%', ProductName)
ELSE 0
END AS MatchedPosition,
CASE
WHEN PATINDEX('%Chef [A-Za-z]%', ProductName) > 0 THEN 'Chef followed by word'
WHEN PATINDEX('%[0-9][gG]%', ProductName) > 0 THEN 'Number followed by g/G'
ELSE 'No Match'
END AS MatchDescription
FROM
Products
WHERE
PATINDEX('%Chef [A-Za-z]%', ProductName) > 0 OR
PATINDEX('%[0-9][gG]%', ProductName) > 0;
Create SQL query with SqlQueryBuilder 2
var (sql2, parameters2) = new SqlQueryBuilder()
.Select()
.Column("ProductID", "ProductID")
.Column("ProductName", "ProductName")
.Column(new CASE()
.When(new PATINDEX("%Chef [A-Za-z]%", new Column("ProductName")).GreaterThan(0))
.Then(new PATINDEX("%Chef [A-Za-z]%", new Column("ProductName")))
.When(new PATINDEX("%[0-9][gG]%", new Column("ProductName")).GreaterThan(0))
.Then(new PATINDEX("%[0-9][gG]%", new Column("ProductName")))
.Else(0), "MatchedPosition")
.Column(new CASE()
.When(new PATINDEX("%Chef [A-Za-z]%", new Column("ProductName")).GreaterThan(0))
.Then("Chef followed by word")
.When(new PATINDEX("%[0-9][gG]%", new Column("ProductName")).GreaterThan(0))
.Then("Number followed by g/G")
.Else("No Match"), "MatchDescription")
.From("Products")
.Where(new Where(new PATINDEX("%Chef [A-Za-z]%", new Column("ProductName")), SQLComparisonOperators.GREATER_THAN, 0)
.OR(new PATINDEX("%[0-9][gG]%", new Column("ProductName")), SQLComparisonOperators.GREATER_THAN, 0)
)
.Build();
Query build by SqlQueryBuilder 2
SELECT ProductID AS ProductID,
ProductName AS ProductName,
CASE WHEN PATINDEX(@pMAIN_2507200132162365020, ProductName) > @pMAIN_2507200132162365021 THEN PATINDEX(@pMAIN_2507200132162365022, ProductName) WHEN PATINDEX(@pMAIN_2507200132162365023, ProductName) > @pMAIN_2507200132162365024 THEN PATINDEX(@pMAIN_2507200132162365025, ProductName) ELSE @pMAIN_2507200132162365026 END AS MatchedPosition,
CASE WHEN PATINDEX(@pMAIN_2507200132162365027, ProductName) > @pMAIN_2507200132162365028 THEN @pMAIN_2507200132162365029 WHEN PATINDEX(@pMAIN_250720013216236502_10, ProductName) > @pMAIN_250720013216236502_11 THEN @pMAIN_250720013216236502_12 ELSE @pMAIN_250720013216236502_13 END AS MatchDescription
FROM Products
WHERE PATINDEX(@pMAIN_250720013216236502_14, ProductName) > @pMAIN_250720013216236502_15
OR PATINDEX(@pMAIN_250720013216236502_16, ProductName) > @pMAIN_250720013216236502_17;
Parameters (If used)
Name |
Value |
@pMAIN_2507200132162365020 |
%Chef [A-Za-z]% |
@pMAIN_2507200132162365021 |
0 |
@pMAIN_2507200132162365022 |
%Chef [A-Za-z]% |
@pMAIN_2507200132162365023 |
%[0-9][gG]% |
@pMAIN_2507200132162365024 |
0 |
@pMAIN_2507200132162365025 |
%[0-9][gG]% |
@pMAIN_2507200132162365026 |
0 |
@pMAIN_2507200132162365027 |
%Chef [A-Za-z]% |
@pMAIN_2507200132162365028 |
0 |
@pMAIN_2507200132162365029 |
Chef followed by word |
@pMAIN_250720013216236502_10 |
%[0-9][gG]% |
@pMAIN_250720013216236502_11 |
0 |
@pMAIN_250720013216236502_12 |
Number followed by g/G |
@pMAIN_250720013216236502_13 |
No Match |
@pMAIN_250720013216236502_14 |
%Chef [A-Za-z]% |
@pMAIN_250720013216236502_15 |
0 |
@pMAIN_250720013216236502_16 |
%[0-9][gG]% |
@pMAIN_250720013216236502_17 |
0 |
Query Results 2:
|
ProductID |
ProductName |
MatchedPosition |
MatchDescription |
1 |
4
|
Chef Anton's Cajun Seasoning
|
1
|
Chef followed by word
|
2 |
5
|
Chef Anton's Gumbo Mix
|
1
|
Chef followed by word
|