gotqn
gotqn

Reputation: 43666

T-SQL Search In String for specific words

Short to the point:

"I am using SQL Server Manager 2008 R2. I have a table with columns "product name" and "product size". The size of the product is recorded in his name like that:

Ellas Kitchen Apple & Ginger Baby Cookies 120g

Ellas Kitchen Apple, Raisin 'n' Cinnamon Bakey Bakies 4 x 12g

Elastoplast Spray Plaster 32.5ml

Ellas Kitchen Stage 1 Butternut Squash Each

the size of this product should be:

120g

4 x 12g

32.5ml

N/A

(some of the products can have no size in there name and should be set to "N/A")

I want to write T-SQL statement that update the product size getting it from the product name.

I have done this in javascript, but in order to do the things right I have to write SQL statement and that's my problem. I have found it very difficult to work with "regular expressions" in T-SQL.

I have seen a exmaple of how to get only the number from string, but have no idea how to do using sql.

Declare @ProductName     varchar(100)
Select     @ProductName=    'dsadadsad15234Nudsadadmbers'

Select     @ProductName=    SubString(@ProductName,PATINDEX('%   [0-9]%',@ProductName),Len(@ProductName))
Select     @ProductName=    SubString(@ProductName,0,PATINDEX('%[^0-9]%',@ProductName))

Select     @ProductName

I will appreciate any example or idea. Thanks, in advance.

EDIT:

Thanks for your reply,xQbert.

I have not included all possible formats, because if I have a working example with few of them I think I will be able to do for all. Anyway, in order to give more details here are the possible situations:

( Inumber + "x" + Dnumber + word)* + (_)* + (Dnumber + word)* - * means 0 or more

where word can be - g, kg, ml, cl, pack where Inumber is integer where Dnumber is double where _ is space

For exmaple:

12 x 100g 100ml 2 x kg

And the price (if there is ) is always in the end of the name:

Product name + product prize

For example:

Organix Organic Chick Pea & Red Pepper Lasagne 190g Organix Organic Vegetable & Pork Risotto 250g Organix Rice Cakes apple 50g Organix Rusks 7m+ 6 Pack Organix Savoury Biscuits Cheese & Onion Each Organix Savoury Biscuits Tomato & Basil Each Organix Stage 1 Squash & Chicken 2 x 120g

Upvotes: 2

Views: 1534

Answers (1)

paparazzo
paparazzo

Reputation: 45106

PATINDEX is not REGX and you have limited logic processing in TSQL compared to .NET. Have you condisidered CLR integration?

  http://msdn.microsoft.com/en-us/library/ms131089(SQL.100).aspx

This from 2005 but and example of REGX in SQL via CLR integration.

  http://msdn.microsoft.com/en-us/library/ms345136(v=SQL.90).aspx

Upvotes: 1

Related Questions