?
Solved

SQL 2008 LIKE Clause

Posted on 2014-11-05
8
Medium Priority
?
83 Views
Last Modified: 2015-03-04
Using a Like Clause, I want to read table A and if I find a match from Table B somewhere in Fld1 of Table A, I want to update Fld1 with the match from Table B.

Table A

Fld1

aaahopaaa
bbbfrogzzz
doghhhhhh
xxbirdxxxz

Table B

Fld2

hop
dog
frog
cat

So when all done, Table A should look like below:

Fld1

hop
frog
dog
xxbirdxxxz
0
Comment
Question by:thayduck
[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
  • 4
  • 3
8 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 40424634
I guess it would be something like the code below but PLEASE run it in a test environment first:

update table_A set Fld1 = b.Fld2
from table_B b
where exists(select * from table_A where Fld1 like '''%'+b.Fld2+'%''')
      and table_A.Fld1 like '''%'+b.Fld2+'%'''
0
 

Author Comment

by:thayduck
ID: 40424731
No match.

update WKLDStaging.RadioWorkRTI set orgloc = g.DeviceName
from WKLDStaging.RadioGeography g
where exists(select * from WKLDStaging.RadioWorkRTI where orgloc like '''%'+g.DeviceName+'%''')
      and WKLDStaging.RadioWorkRTI.orgloc like '''%'+g.DeviceName+'%'''


orgloc actually =  (_o__ RTI 4149(Amos RTI))HDX-2 Telco
There is a record with a device name =  Amos RTI
0
 

Author Comment

by:thayduck
ID: 40424805
This worked:

update WKLDStaging.RadioWorkRTI set orgloc = g.DeviceName
from WKLDStaging.RadioGeography g
where exists(select * from WKLDStaging.RadioWorkRTI where orgloc like '%' + g.DeviceName + '%')
      and WKLDStaging.RadioWorkRTI.orgloc like '%' + g.DeviceName + '%'
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 40

Accepted Solution

by:
lcohan earned 1000 total points
ID: 40424818
Yeah sorry...I had too many quotes as I ripped it out of a dynamic string.

create table table_A (Fld1 sysname);
create table table_B (Fld2 sysname);

insert into table_A select 'aaahopaaa';
insert into table_A select 'bbbfrogzzz';
insert into table_A select 'doghhhhhh';
insert into table_A select 'xxbirdxxxz';

insert into table_B select 'hop';
insert into table_B select 'dog';
insert into table_B select 'frog';
insert into table_B select 'cat';

select * from table_A
select * from table_B

update table_A set Fld1 = b.Fld2
from table_B b
where exists(select * from table_A where Fld1 like '%'+b.Fld2+'%')
            and table_A.Fld1 like '%'+b.Fld2+'%'

select * from table_A
select * from table_B
0
 

Author Comment

by:thayduck
ID: 40424835
I accepted your solution but it does not show up as accepted.
I did it twice.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 total points
ID: 40424930
You don't need two separate comparisons [an exists with a like and an expression with a like], a single join will do it:


update w
set orgloc = g.DeviceName
from WKLDStaging.RadioWorkRTI w
inner join WKLDStaging.RadioGeography g on
    w.orgloc like '%' + g.DeviceName + '%'
0
 
LVL 40

Expert Comment

by:lcohan
ID: 40425117
That's fine - glad you got your work done.
0
 

Author Comment

by:thayduck
ID: 40425877
Thanks Scott, your code worked to.
Less code is always good.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

770 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