Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old March 18th, 2004, 07:27 AM
nigelm nigelm is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 nigelm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Using LIKE comparison with update queries

Anyone know if it's possible to update records in a MS SQL table using the LIKE comparison in conjunction with records from another table.

For example, in one table I have a list of company names together with a client number, similar data is held in another table, same fields, but the company names are slightly different (e.g. ABC Limited / ABC Ltd) in some way. I want to be able to update the client number in one table (currently null) with the client number from the other table using a LIKE comparison on the company names.

In simple terms, I want the following "dummy" syntax to work:

UPDATE ...
SET ....
FROM ...
WHERE [CompanyName1] LIKE [CompanyName2]

This works if a use a string value (e.g. 'ABC Lim%') or if I use the equals comparison instead, but I want to perform the update where records are similar.

Any clues??

Reply With Quote
  #2  
Old March 18th, 2004, 10:03 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
You would need to extract a portion of the company name. If you ran a sql update statement like this
Code:
UPDATE Table2
SET Fieldname = value
WHERE CompanyName LIKE 'ABC Limited%'

It wouldn't update anything, because there is not record in table2 with a company name like that.

you could do it like this
Code:
myVar = "ABC Limited"

strSQL = "UPDATE Table2 SET FieldName = value WHERE CompanyName LIKE '" & Left(myVar, 3) & "%'"

Which would update any record where the Company name begins with "ABC".

Reply With Quote
  #3  
Old March 22nd, 2004, 02:55 PM
Lord Merlin Lord Merlin is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 Lord Merlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question

I've got a bit more complicated scenario

I got two tables, comments, and companies, now I want to update the subscribers fields in the comments table, if the company exists in both the comments table, as well as the companies table

How would I do this?

I tried "UPDATE comments SET subscribers = 'Y' WHERE supplier = companies.company_name"

But this doesn't seem to work?

Reply With Quote
  #4  
Old March 22nd, 2004, 04:27 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
You would have to hold the company name in a variable.
Or
Do a larger sql statment that actually grabs the company name as well.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Using LIKE comparison with update queries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT