0

I have a table with column name that has random characters before and after the name ie:

Table A:

  Name
  -----------------
  asd4345JONlkj345
  .;lidDavidlksd$

and I have another table in same DB that has the names ie:

Table B:

 Name
 ------
 David
 Jon

This goes on like this for 30k rows or I'd just hardcode something really quick. I want to search each string in Table A 'Name' column for each value from Table B, and if found return the name in a new column.

I have a feeling this will be a UDF, which is fine, I'm just unsure how to use patindex in this scenario, or if that is even the right approach.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3486773
  • 1,174
  • 3
  • 25
  • 50
  • 1
    I don't understand a down vote in less than a min with no explanation why? how would one even have time to understand the problem in such short amount of time? – user3486773 Nov 01 '16 at 21:21
  • The downvote did not come from me but I am sure it because it is not really clear what you are trying to do here. We can get a general sense but code doesn't work like that. We need actual specifics. – Sean Lange Nov 01 '16 at 21:24
  • Here is a great place to start making your question better. http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ – Sean Lange Nov 01 '16 at 21:25

2 Answers2

3

You only need the LIKE operator for this:

select * 
from TableA
inner join TableB on TableA.Name like '%' + TableB.Name + '%'
SlimsGhost
  • 2,849
  • 1
  • 10
  • 16
1
  select B.Name, A.Name from tableA A inner join join tableB B
  on rtrim(ltrim(B.Name)) like '%' + rtrim(ltrim(A.Name)) + '%'
Esperento57
  • 16,521
  • 3
  • 39
  • 45