简体   繁体   English

带有 2 个分隔符的 SUBSTRING_INDEX

[英]SUBSTRING_INDEX with 2 delimiters

I have 2 different types of item codes.我有 2 种不同类型的项目代码。 It consist of 2 parts, [companycode]-[itemcode] .它由两部分组成, [companycode]-[itemcode] Below is the example:下面是示例:

A: 0888-DIA172966616 B: 0888-PMA516998-2200000M A: 0888-DIA172966616 B: 0888-PMA516998-2200000M

I want to substring only the [itemcode] parts of it so I use我只想 substring 的[itemcode]部分所以我使用

SUBSTRING_INDEX(ItemCode, '-', -1)

It works fine with the A item code DIA172966616 , but for the B, it gives me only the value from the second "-", so it's just 2200000M .它适用于 A 项目代码DIA172966616 ,但对于 B,它只给我第二个“-”的值,所以它只是2200000M How to get the full item code for the B?如何获得 B 的完整项目代码?

SUBTRING_INDEX() is only useful if you want the first N or last N delimited strings, you can't use it directly for "all but first N". SUBTRING_INDEX()仅在您想要前 N 或最后 N 分隔字符串时才有用,您不能直接将其用于“除前 N 之外的所有字符串”。

Don't use SUBSTRING_INDEX() for this, use SUBSTR() and LOCATE()不要为此使用SUBSTRING_INDEX() ,使用SUBSTR()LOCATE()

SUBSTR(ItemCode, LOCATE('-', ItemCode)+1)

LOCATE(ItemCode, '-') returns the position of the first - , and SUBSTR() then returns everything after that. LOCATE(ItemCode, '-')返回第一个-的 position ,然后SUBSTR()返回之后的所有内容。 +1 makes it skip over the - as well. +1也使它跳过-

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

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