Pages

Thursday, March 31, 2011

How to use PATINDEX for complex search?

How to use PATINDEX for complex search?

Execute the following Microsoft SQL Server T-SQL script in SSMS Query Editor to demonstrate the use of PATINDEX for complex search. The following search will find all bikes which off-road yet comfortable.
use AdventureWorks2008;
select ProductName=[Name], ProductNumber, [Description], ListPrice
from [Production].[Product] p
join [Production].[ProductModelProductDescriptionCulture] pmpdc
on p.ProductModelID = pmpdc.ProductModelID
join [Production].[ProductDescription] pd
on pmpdc.ProductDescriptionID = pd.ProductDescriptionID
where patindex( ‘%off-road%comfortable%’, pd.[Description]) > 0
Partial results:

ProductNameProductNumberDescriptionListPrice
Mountain-500 Silver, 40BK-M18S-40Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride.564.99
Mountain-500 Silver, 42BK-M18S-42Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride.564.99
Mountain-500 Silver, 44BK-M18S-44Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride.564.99
Mountain-500 Silver, 48BK-M18S-48Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride.564.99
Mountain-500 Silver, 52BK-M18S-52Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride.564.99
Mountain-500 Black, 40BK-M18B-40Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride.539

No comments:

Post a Comment