I need help to create an update statement with select and case involved

Posted on 2011-09-07
Last Modified: 2012-05-12

I need to do an update to a Company column in my database.

Basically I have a lookup table like so

Lookup Table
Category eg TravelCompany,AccommodationCompany etc
Description eg The names of the companies

and another table that has companies in it like so

Accommodation table

Now the problem is that in the Accommodation table the companies vary slightly from the lookup table values. The CompanyID column is a new column and does not have any data in it at present so this is the reason for the update. I hope this is starting to make some sense, I need a kind of update select case procedure that will allow me to insert the correct id's into Accommodation table.

Your help would be appreciated

Question by:jazz__man
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
  • 2
  • 2
  • 2
  • +2

Expert Comment

ID: 36495594
UPDATE Accommodation
SET CompanyID = (SELECT LookupID FROM Lookup WHERE Lookup.Description = Accommodation.Company)


UPDATE Accommodation
SET CompanyID = Lookup.LookupID
FROM Accommodation acc
   INNER JOIN Lookup ON acc.Company = Lookup.Description

These queries were written assuming Lookup table does not contain duplicate Descriptions.

Author Comment

ID: 36495635
what about where Lookup.Description <> Accommodation.Company

Expert Comment

ID: 36495690
Hi Jazz man,

from my understanding you effectively have dirty data, i.e. the names of the companies vary slightly to the descriptions, is this correct?

If so, depending on how involved you want to go there are a number of possible solutions but no "black & white" answer.

The easiest thing to do is probably use an SSIS fuzzy lookup component to match the ones that arent exactly the same.

Beyond that you are getting into the realms of doing things like leveinsten differences and phonetics


SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.


Expert Comment

ID: 36495692
Cases like that will have to be handled manually, OR using soundex:
LVL 32

Expert Comment

ID: 36495710
Can you provide some sample data for the two tables and what the accommodation table should look like after the update?

Author Comment

ID: 36496019

Thats exactly what I have...dirty data.

To my knowledge I need some kind of case statement

Accepted Solution

DavidMorrison earned 500 total points
ID: 36496287
Jazz man,

If you had a list of all possible combinations of the company name and what they should match to then a IN statement might work rather than a case, although I get the impression from your post that you don't have this mapping list?

As I said before, your best bet is SSIS, pipe it through a fuzzy lookup component, this will get you so far. other than that it will be a manual process. If you only have to do this once and the data volumes arent that large then I'd just bite the bullet and whack it excel and do it by hand, creating a mapping table which you could then load into sql and just do a simple join.

As I said unfortunately there is no real "black and white" answer to fuzzy matching
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 36499238
Sorry for hijacking,

Jazz_man, can you please clarify the issue @

(that you asked some time ago, which opened and closed a few times, and you never replied)

EE admins, is there anyway to send private message to members?


Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySql Workbench outputting "OK" as result of queries. 5 50
Database Owner 3 45
Please explain Equi-join 3 44
Set a variable value in SQL Procedure 3 26
In this article I will describe the Detach & Attach 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.
In this article I will describe the Backup & Restore 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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

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