Solved

Find the values of filed from one table searching through records in a second table

Posted on 2014-03-23
10
192 Views
Last Modified: 2014-03-30
Hi Experts,

I have 2 tables.
Tb1 has ID1 and ID2 field , char (6) .
Tb2 has ID1 and Comment field.
I need to search through records of Tb2..comment  varchar (4000) and find values of ID2 and replace by ID1.


ID1 is numeric and ID2 is alphanumeric.

Example data:
tb1
---------
ID1     ID2
=============
999999  Oe123b
888888  e9874b

tb2
=================
ID1        Comment
234560  the entry is very similar to Oe123b. --->replace Oe123b with 999999
111111  See e9874b.                                        --->replace e9874b  with 888888


This code can’t even identify the entries that contain ID2.


DECLARE @ID2 CHAR(6)
--Set values for search  
set @ID2 = 'SELECT ID2 from tb1 where ID2 is not null'


SELECT DISTINCT a.ID1,a.Comment
FROM tb2  as a,
tb1 as b
WHERE a.Comment LIKE '% '+ @ID2 +'%'
AND b.ID2 is not null

Is there some way of using the replace function to update all instances.
Thank you.
0
Comment
Question by:PDF
  • 4
  • 3
  • 3
10 Comments
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39950433
Try this:
UPDATE a SET
	Comment=REPLACE(Comment,b.ID2,b.ID1)
FROM 
	tb2  as a,
	inner join tb1 as b
		ON	a.Comment LIKE '% '+ b.ID2 +'%' 
		AND b.ID2 is not null

Open in new window

To check first you can run:
select
	a.ID1 as tb2ID,
	a.Comment as CommentBefore
	REPLACE(Comment,b.ID2,b.ID1) as CommentAfter,
	b.ID1,
	b.ID2
FROM 
	tb2  as a,
	inner join tb1 as b
		ON	a.Comment LIKE '% '+ b.ID2 +'%' 
		AND b.ID2 is not null

Open in new window

0
 

Author Comment

by:PDF
ID: 39950537
@ Zberteoc,

Thanks for the comment,
But , as I said my  query doesn’t do the job,
First I need to identify records where is update needed.
0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 250 total points
ID: 39950622
OK, a couple of things to be careful with like char(6) with trailing spaces, or longer strings in the comment with a prefix matching the ID2 (e.g. '123456' will match with '12345678')

Having said that, the following works with the test scenario above...

-- first create some test tables

create table #TB1 (ID1 char(6), ID2 char(6))
create table #TB2 (ID1 char(6), Comment varchar(4000))

-- now insert our test data

insert #TB1 values ('999999', 'Oe123b')
insert #TB1 values ('888888', 'e9874b')

insert #TB2 values ('234560', 'the entry is very similar to Oe123b.') --->replace Oe123b with 999999
insert #TB2 values ('111111', 'See e9874b.')                          --->replace e9874b  with 888888
 
-- then run a test query

 select T2.*, T1.* 
 from #TB2 T2
 cross apply (select * from #TB1 T1 where charindex(T1.ID2, T2.Comment) > 0) T1

-- now do the update

 update T2 set comment = replace(comment,T1.id2,T1.id1)
 from #TB2 T2
 cross apply (select * from #TB1 T1 where charindex(T1.ID2, T2.Comment) > 0) T1

-- and finally, check the results

select * from #TB2

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:PDF
ID: 39950674
Works beautifully on a test data.
Let me do on real tables.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39950730
@PDF

I don't think you understood my solution. The update query will do the job in 1 step without any loops. What is the problem with that?
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 39951042
Can confirm that Zberteoc's solution also works.

Small syntax error on the JOIN (remove the comma prior to the join, after the table alias 'a')
And a missing comma after the 'commentbefore' in the select.

Having just tested and fixed those small typo's, then I can confirm it does work as Zberteoc intended.

As for a loop, you might need one if there are multiple / different instances of ID2 within the comment.

For my code, as with Zberteoc's, you could encapsulate within a "while" loop using the select to test for a result / record count > 0

If that is the case, or a possibility, then the "while" loop would be worth considering.
0
 
LVL 26

Accepted Solution

by:
Zberteoc earned 250 total points
ID: 39951099
Thanks Mark,

Indeed that was a typo, actually I forgot to remove the comma after I modified the original query.  

As for your concern for the multiple tb2 ID2 occurrences it is only valid if there would be a rule saying that only the first or last occurrence of a matched value of ID1 would be preferred. In the case of UPDATE solution I gave, will still work but with one of the ID1 values that will be matched to a multiple ID2 value(could be the last one after joining).

The UPDATE, now corrected, will work perfectly:
UPDATE a SET
	Comment=REPLACE(Comment, b.ID2, b.ID1)
FROM 
	tb2  as a
	inner join tb1 as b
		ON	a.Comment LIKE '% '+ b.ID2 +'%' 
		AND b.ID2 is not null

Open in new window

And to check before update you use:
SELECT
	a.ID1 as tb2ID,
	a.Comment as CommentBefore,
	REPLACE(Comment,b.ID2,b.ID1) as CommentAfter,
	b.ID1,
	b.ID2
FROM 
	tb2  as a
	inner join tb1 as b
		ON	a.Comment LIKE '% '+ b.ID2 +'%' 
		AND b.ID2 is not null

Open in new window

0
 

Author Comment

by:PDF
ID: 39951115
Thank you @ Zberteoc & Mark !!!!

Now I have a dilemma how to fairly split points.
Any ideas or suggestions???
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 39951128
I would be happy with sharing half and half...

But entirely up to you - it is your question :)
0
 

Author Closing Comment

by:PDF
ID: 39965495
Thank you @ Zberteoc & Mark !!!!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

839 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