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
Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert null in sql server 12 45
query question 12 32
Trouble with <> 2 20
question about results where i dont have a match 3 20
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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