简体   繁体   English

如何根据Oracle中另一列的内容删除列中的部分字符串

[英]How to remove part of string in a column based on content of another column in Oracle

I have database tables that look like this:我有如下所示的数据库表:

Class   | ClassNumber
S       | S3
T       | T37
T       | T50
S       | SS07
S       | S4
S       | SG27

ClassNumber contains the value of Class and another identifier. ClassNumber包含Class的值和另一个标识符。 So meaning that if the Class is S , then it means ClassNumber must begin with S and followed by another identifier.这意味着如果ClassS ,则意味着ClassNumber必须以S开头,后跟另一个标识符。

I would like to extract the identifier in ClassNumber .我想提取ClassNumber的标识符。

If I use the REPLACE function, it will replace all characters that match.如果我使用REPLACE函数,它将替换所有匹配的字符。 But I only want the prefix to be removed.但我只想删除前缀。

SELECT REPLACE(ClassNumber, Class, '') FROM MY_TABLE

This will make SS07 to be 07 instead.这将使SS07改为07 But I want it to return S07 .但我希望它返回S07

How do I do this?我该怎么做呢?

Try REGEXP_REPLACE尝试REGEXP_REPLACE

SELECT CLASSNUMBER,REGEXP_REPLACE(ClassNumber, '^'||Class, '') as id FROM t

Or if you are sure that it's always a single digit class, simply use或者,如果您确定它始终是个位数的类,只需使用

SUBSTR(CLASSNUMBER,2)

Demo 演示

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

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