简体   繁体   English

SQL串联查询

[英]SQL Concatenation Query

I have a column called I_Tags and im trying to compare it to another table Asset_Tags . 我有一列称为I_Tags ,我试图将其与另一个表Asset_Tags进行比较。

I_Tags has fields like I_Tags具有类似

PV-B02-0115
B04-PV-7126B--
B05-PV0841A--
B05-PV-0841B--
B09-PV-1155A--

Asset_Tags have fields like Asset_Tag的字段如下

PV-B04-6888
PV-B05-0841A--
PV-B05-1219
PV-B09-1155A--

Im unsure how to write a case statement based on the following scenarios: 不知道如何根据以下方案编写case语句:

  1. If the first two characters in I_Tag are letters PV, match the entire string for matches with Asset_Tags 如果I_Tag中的前两个字符是字母PV,则将整个字符串与Asset_Tags进行匹配
  2. If not, and PV is 4th & 5th characters in I_Tag , move them to the 1st & 2nd character locations and remove any/all number of the dashes (-) at the end and match with Asset_Tags . 如果不是,并且PV是I_Tag第4个和第5个字符,请将其移动到第1个和第2个字符位置,并删除末尾的任意/所有破折号(-)并与Asset_Tags匹配。

I have never written a case statement in SQL so Im unsure how to write a query for this. 我从未在SQL中编写过case语句,所以不确定如何为此编写查询。

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you 谢谢

Maybe this helps, if returns the result so that PV is always at the start and removes the trailing -s: 如果返回结果,使PV始终位于开头并删除结尾的-s,则可能会有所帮助:

select
  case 
    when I_Tags like 'PV%' then I_Tags
    when I_Tags like '____PV%' then
      'PV-'+left(I_Tags, 3) + 
      substring(I_Tags, 7, len(I_Tags) -5 -patindex('%[^-]%', reverse(I_Tags)))
    end as test
from
  table1

You can test it in SQL Fiddle: http://sqlfiddle.com/#!6/526db/16 您可以在SQL Fiddle中对其进行测试: http ://sqlfiddle.com/#!6/526db/16

Not sure if there was a typo in your example with B05-PV0841A--, but it leaves it without the -. 不知道您的示例中是否有B05-PV0841A--的错字,但它没有-。

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

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