Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Looking for pattern in field from other table

Author  Topic 

cygnusx197
Starting Member

1 Post

Posted - 2009-01-21 : 15:35:31
If I have a lookup table like:

Apple
Orange
Grapefruit

and a column in another table with field values like this (call it names):
123123_Apple
BRR4234_Apple_1.pdf
Apple_123123
Orange_33.pdf

What functions can I use to see if a lookup value exists somewhere inside the names field's string?
I've seen the substring function but I don't know the starting and ending positions, and the use of underscores and numbers is way to complicated attack it like a regular expression cleanup exercise.
(I'm weak on regex's anyway)

I'm trying to replace the names field with its lookup value if it matches and leave it alone if not.
I'm not sure you can use a subquery combined with an IN and LIKE so I could use a little help putting this query together.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-01-21 : 16:02:33
Assuming your first table is called Lookup and the column name is Fruit, and the second table is called Names and the column name is name,

update Names set [name] = l.Fruit from Lookup l inner join Names n on n.name like '%' + l.Fruit + '%'

If you had a row in the Names table that had multiple fruits, for example 'Orange_33_Apple.pdf', the outcome is unpredictable.
Go to Top of Page
   

- Advertisement -