简体   繁体   English

Oracle 8,SQL:用于字符串操作的RTRIM无法按预期方式工作

[英]Oracle 8, SQL: RTRIM for string manipulation is not working as expected

Oracle 8 SQL: I do have data like "VTR 564-31 / V16 H12 W08 E19 L14" from which I want to trim the second part => "VTR 564-31" Oracle 8 SQL:我确实有类似“ VTR 564-31 / V16 H12 W08 E19 L14”的数据,我想从中修剪第二部分=>“ VTR 564-31”

According to this website I can use the rtrim function 根据这个网站,我可以使用rtrim功能

rtrim('123000', '0'); rtrim('123000','0'); would return '123' 将返回“ 123”

like this it works, but adapted to my use case, the following one does not trim at all? 像这样工作,但是适合我的用例,下面的一个根本不适合吗? Do I have to escape the special character??? 我是否必须逃脱特殊字符???

rtrim('VTR 564-31 / V16 H12 W08 E19 L14',' / ') rtrim('VTR 564-31 / V16 H12 W08 E19 L14','/')

RTRIM removes characters specified in the second parameter from the end of the string specified in the first. RTRIM从第一个参数中指定的字符串的末尾删除第二个参数中指定的字符。 Since the last character of 'VTR 564-31 / V16 H12 W08 E19 L14' is a '4', which is not specified in the second parameter ' /', there is nothing to trim. 由于'VTR 564-31 / V16 H12 W08 E19 L14'的最后一个字符是'4'(未在第二个参数'/'中指定),因此没有任何内容可修剪。

It looks like you think it looks for the first occurence of ' /' in the first string and removes everything from there on, but that's not what it does. 好像您认为它在第一个字符串中查找“ /”的第一个匹配项,并从那里删除所有内容,但这不是它的作用。

For example: 例如:

SQL> select rtrim('AAABBBCCCBBBAAA','AB') from dual;

RTRIM('AA
---------
AAABBBCCC

RTRIM removed all the As and Bs from the end of the string. RTRIM删除了字符串末尾的所有As和B。

Probably what you actually want is: 您实际想要的可能是:

select substr(yourstring, 1, instr(yourstring, ' / ')-1) from dual; 

ie use INSTR to locate the position of ' / ' and then SUBSTR to get just the part of "yourstring" before that. 即使用INSTR定位'/'的位置,然后使用SUBSTR来获取之前的“您的字符串”部分。

What you want is something like: 您想要的是这样的:

SELECT RTRIM(SUBSTR('VTR 564-31 / V16 H12 W08 E19 L14',1,INSTR('VTR 564-31 / V16 H12 W08 E19 L14','/')-1),' ')
FROM DUAL;

The INSTR function locates the '/' in your string value, the SUBSTR function extracts the characters from the start of the string to the character immediately before the '/' located by INSTR, while the RTRIM removes any spaces that had occurred before the '/' INSTR函数在字符串值中定位“ /”,SUBSTR函数将字符串开头的字符提取到INSTR定位的“ /”之前的字符,而RTRIM则删除“ /'

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

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