DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to use PATINDEX for complex search?

Execute the following Microsoft SQL Server T-SQL scripts 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:

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

 

------------

 

------------

-- NUMERIC and ALPHA pattern search with PATINDEX

------------

select ProductNumber from AdventureWorks2008.Production.Product

where patindex('%[0-9][A-Z]%',ProductNumber) > 5

/* ProductNumber

BK-M18B-40

BK-M18B-42

BK-M18B-44   ****/

------------

Related article:

PATINDEX (Transact-SQL)

 

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE