Solved

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

Posted on 2008-10-26
3
202 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 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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