在 SQL Server 字符串中搜索另一个表中的值 [英] Search SQL Server string for values from another table

查看:33
本文介绍了在 SQL Server 字符串中搜索另一个表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列 name 的表格,它在名称前后有随机字符,即:

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

表 A:

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

我在同一个数据库中有另一个表,其名称为:

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

表 B:

 Name
 ------
 David
 Jon

对于 30k 行,这样继续下去,或者我只是快速地硬编码一些东西.我想在表 A 的名称"列中的每个字符串中搜索表 B 中的每个值,如果找到,则在新列中返回名称.

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.

我有一种感觉,这将是一个 UDF,这很好,我只是不确定如何在这种情况下使用 patindex,或者这是否是正确的方法.

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.

推荐答案

你只需要 LIKE 操作符:

select * 
from TableA
inner join TableB on TableA.Name like '%' + TableB.Name + '%'

这篇关于在 SQL Server 字符串中搜索另一个表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆