Oracle 8、SQL:用于字符串操作的 RTRIM 未按预期工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3108249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle 8, SQL: RTRIM for string manipulation is not working as expected
提问by Werner
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 websiteI can use the rtrim function
根据这个网站,我可以使用 rtrim 功能
rtrim('123000', '0'); would return '123'
rtrim('123000', '0'); 将返回“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',' / ')
回答by Tony Andrews
RTRIM removes characters specified in the second parameter from the end of the string specified in the first. 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.
RTRIM 从第一个参数中指定的字符串的末尾删除第二个参数中指定的字符。由于'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 endof the string.
RTRIM 删除了字符串末尾的所有 As 和 B。
Probably what you actually want is:
可能你真正想要的是:
select substr(yourstring, 1, instr(yourstring, ' / ')-1) from dual;
i.e. use INSTR to locate the position of ' / ' and then SUBSTR to get just the part of "yourstring" before that.
即使用 INSTR 来定位 ' / ' 的位置,然后使用 SUBSTR 在此之前获取“你的字符串”的一部分。
回答by Mark Baker
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 删除在 ' 之前出现的任何空格/'