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_2602032111302039340, CompanyName) AS RestaurantPosition
FROM Customers
WHERE PATINDEX(CONCAT(@pMAIN_2602032111302039341, @pMAIN_2602032111302039342, @pMAIN_2602032111302039343), CompanyName) > @pMAIN_2602032111302039344;
Parameters (If used)
| Name |
Value |
| @pMAIN_2602032111302039340 |
%Restaurant% |
| @pMAIN_2602032111302039341 |
% |
| @pMAIN_2602032111302039342 |
Restaurant |
| @pMAIN_2602032111302039343 |
% |
| @pMAIN_2602032111302039344 |
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_2602032111302071140, ProductName) > @pMAIN_2602032111302071141 THEN PATINDEX(@pMAIN_2602032111302071142, ProductName) WHEN PATINDEX(@pMAIN_2602032111302071143, ProductName) > @pMAIN_2602032111302071144 THEN PATINDEX(@pMAIN_2602032111302071145, ProductName) ELSE @pMAIN_2602032111302071146 END AS MatchedPosition,
CASE WHEN PATINDEX(@pMAIN_2602032111302071147, ProductName) > @pMAIN_2602032111302071148 THEN @pMAIN_2602032111302071149 WHEN PATINDEX(@pMAIN_260203211130207114_10, ProductName) > @pMAIN_260203211130207114_11 THEN @pMAIN_260203211130207114_12 ELSE @pMAIN_260203211130207114_13 END AS MatchDescription
FROM Products
WHERE PATINDEX(@pMAIN_260203211130207114_14, ProductName) > @pMAIN_260203211130207114_15
OR PATINDEX(@pMAIN_260203211130207114_16, ProductName) > @pMAIN_260203211130207114_17;
Parameters (If used)
| Name |
Value |
| @pMAIN_2602032111302071140 |
%Chef [A-Za-z]% |
| @pMAIN_2602032111302071141 |
0 |
| @pMAIN_2602032111302071142 |
%Chef [A-Za-z]% |
| @pMAIN_2602032111302071143 |
%[0-9][gG]% |
| @pMAIN_2602032111302071144 |
0 |
| @pMAIN_2602032111302071145 |
%[0-9][gG]% |
| @pMAIN_2602032111302071146 |
0 |
| @pMAIN_2602032111302071147 |
%Chef [A-Za-z]% |
| @pMAIN_2602032111302071148 |
0 |
| @pMAIN_2602032111302071149 |
Chef followed by word |
| @pMAIN_260203211130207114_10 |
%[0-9][gG]% |
| @pMAIN_260203211130207114_11 |
0 |
| @pMAIN_260203211130207114_12 |
Number followed by g/G |
| @pMAIN_260203211130207114_13 |
No Match |
| @pMAIN_260203211130207114_14 |
%Chef [A-Za-z]% |
| @pMAIN_260203211130207114_15 |
0 |
| @pMAIN_260203211130207114_16 |
%[0-9][gG]% |
| @pMAIN_260203211130207114_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
|