SQL - Regex - Practice Queries

All examples are base don the AdventureWorksLT database from Microsoft.

The table is SalesLt.Product.

You can download it here -

https://docs.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver16&tabs=ssms


Select * from SalesLT.Product


-- % - Any string of zero or more characters.

--     Name with 'Bike'

Select * from SalesLT.Product

WHere Name like '%Bike%'


-- _ (underscore) Any single character.

--   Second character is 'R'

Select * from SalesLT.Product 

where ProductNUmber like '_R%'


--   Third character is '-' and 4th character is 'U'

Select * from SalesLT.Product

where ProductNumber like '__-U%'


--   Check for Product Number pattern

--   Any 2 characters followed by '-' followed

--   by any 4 characters followed by '-' followed

--   by any single character  - HL-U509-R

Select * from SalesLT.Product 

Where ProductNUmber like '__-____-_'


-- [ ] Any single character within the specified range

--     Above pattern with last char L or M

Select * from SalesLT.Product 

Where ProductNUmber like '__-____-[LM]'


--    Above pattern with last char between L and S

Select * from SalesLT.Product 

Where ProductNUmber like '__-____-[L-S]'


--    Above pattern with last char between L and R

Select * from SalesLT.Product 

Where ProductNUmber like '__-____-[L-R]'


-- [^] Any single character not within the specified range

--      Above pattern with last char not between L and R

Select * from SalesLT.Product 

Where ProductNUmber like '__-____-[^L-R]'


--      Product number not starting with F

Select * from SalesLT.Product 

Where ProductNUmber like '[^F]%'


--     Product number not starting with F or H

Select * from SalesLT.Product 

Where ProductNUmber like '[^FH]%'


--     Product NUmber not starting between A to H. 

Select * from SalesLT.Product 

Where ProductNUmber like '[^A-H]%'


-- Example 1 - Data(Size) is Only One Character and Value is from A to Z

Select * from SalesLT.Product 

where Size like '[A-Za-z]'


-- Example 2 - Data(Size) is Two Characters and Values are from A to Z

 

 Select * from SalesLT.Product 

where Size like '[A-Za-z]_'


-- Example 3 - Text(rowguid) Rows Starting with a Number

 Select * from SalesLT.Product 

where rowguid like '[0-9]%'

 

-- Example 4 - Find a Number Pattern -

--             Product Number with 4th and 5th chars as numbers

 Select * from SalesLT.Product 

where ProductNumber like '___[0-9][0-9]%'


-- Example 5 - Number(List Price) with two decimal places ending in 2 

Select * from SalesLT.Product 

where ListPrice like '%.[0-9]2'


-- Using the Not Character for Regex with T-SQL

-- Product Number - after 1st hyphen has either numbers or special characters

Select * from SalesLT.Product 

where ProductNumber like '__-[^A-Z]%'


-- Example - Name has Only alphabetic characters

Select * from SalesLT.Product 

where Name like '%[^A-Z]%'


Select * from SalesLT.Product 

where Name not like '%[^A-Z]%'


-- Example - Name has No special characters


Select * from SalesLT.Product 

where Name like '%[^A-Z0-9]%'


Select * from SalesLT.Product 

where Name not like '%[^A-Z0-9]%' and 

ProductNumber not like '%[^A-Z0-9]%'


Post a Comment

Previous Post Next Post

Contact Form