简体   繁体   English

在SQL中使用字符串的一部分搜索匹配的字符串

[英]search for a matching string using part of the string in SQL

I current have one table whereby with a unique index of 'col_0' I have the same set of data for 2 different indexes. 我目前有一个表,其中具有唯一索引'col_0',我具有2个不同索引的相同数据集。 The problem is the copy function in my application automatically cuts of the description and adds "copied from at the end". 问题是我的应用程序中的复制功能会自动剪切描述并添加“末尾复制”。

Example

Col_0 Description Col_0说明
A This is a very annoying application problem 答:这是一个非常烦人的应用程序问题
B This is a very annoy (Copied from column a) B这很烦人(从a列复制)

I just wanted to search for and then update B so it matches I've tried join it by part of the description. 我只是想搜索然后更新B,因此它与我尝试通过描述的一部分加入的匹配。

So far I've tried CHARINDEX but I can't quite get it right. 到目前为止,我已经尝试了CHARINDEX,但是我做得还不够。

I read your request thus: "Copied from column a)" means copied from the description of the record with col_0 = 'A'. 因此,我阅读了您的请求:“从a)列复制”是指从记录的描述中复制col_0 ='A'。 And you want to update all "copied from .." descriptions with their original description. 您想用其原始描述更新所有“从..复制”描述。

In order to find the "copied from .." records I am using LIKE. 为了找到“从..复制”记录,我正在使用LIKE。 To extract the col_0 value from the string I am using some string functions (mainly REVERSE + CHARINDEX to find the last occurrence of a blank and then SUBSTRING for the extraction). 为了从字符串中提取col_0值,我使用了一些字符串函数(主要是REVERSE + CHARINDEX以查找最后出现的空白,然后用SUBSTRING进行提取)。

update mytable upd
set description =
(
 select description
 from mytable orig
 where orig.col_0 = 
   substring(upd.description, 
             len(upd.description) - charindex(' ', reverse(upd.description)) + 2, 
             charindex(' ', reverse(upd.description) - 2
            )
)
where description like '%(Copied from column %)';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM