Solved

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

Posted on 2008-10-26
3
195 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
3 Comments
 
LVL 5

Accepted Solution

by:
PaulKeating earned 125 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now