?
Solved

T-SQL How to look for part of a datafield in a subquery

Posted on 2008-10-26
3
Medium Priority
?
204 Views
Last Modified: 2010-04-21
I have a question regarding how to build a t-sql query.

I want to get a list of Products that is part of a given brand.

The problem is that I have two tables named 'Products' and 'Brands'. The Products table contain among others the fields 'Name' and 'BrandId'. The Brand table has the fields 'BrandId', 'BrandName', 'LanguageID'. The last field is there because the brands are written in four different languages.
Now for some reson the BrandId foreign key has never been implemented, so all the BrandId fields are 0. However the Products.Name field often will have the Brand name in the text, like "Ralph Lauren Glamourous Daylight Eau De Toilette Spray 50ml/1.7oz".

It's no big deal to search for a given brand name, like:
SELECT * FROM [Product] as P
where P.Name like '%Hugo Boss%'

But I was thinking if there was a way to build a query that finds the Brand relating to a given product and then updates the BrandId reference?
To do this manually would require a lot of hours because the product table contains thousands of lines.
0
Comment
Question by:ThomasCookScandinavia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
PaulKeating earned 500 total points
ID: 22806808
I don't have your tables so I can't check it properly, but give this a try:

update Products
set BrandId = (select max(b.BrandId) from Brand b where charindex(b.BrandName,Products.Name) > 0)
0
 
LVL 9

Expert Comment

by:jamesgu
ID: 22807205
update products
set brandid = b.brandid
from Brand b
where products.brandname = b.brandname
and b. LanguageID = 'eng'

0
 

Author Closing Comment

by:ThomasCookScandinavia
ID: 31510066
This is great. Thanks a lot.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question