SQL Oracle 修剪字符串内部的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21484677/
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 trim whitespace on the inside of a string
提问by Alex
I am storing phone numbers as VARCHAR2 in my system to allow for users to input '+' characters infront of their phone number if they so choose.
我在我的系统中将电话号码存储为 VARCHAR2,以允许用户在他们的电话号码前输入“+”字符(如果他们愿意的话)。
My regexp allows for this perfectly, but when storing the number in the database I want to strip out all whitespace the user may enter.
我的正则表达式完美地实现了这一点,但是当将数字存储在数据库中时,我想去掉用户可能输入的所有空格。
My regexp allows for the following formats
我的正则表达式允许以下格式
+4470123456789
+447 0123456789
+447 01234 56789
01234567890
01234 567890
01234 567 890
I know I could resolve my issue by not letting users put any whitespace in their numbers, but I know from personal experience just how frustrating it is to have a validation error due to some silly formatting on the client side. I have tried using the TRIM function on my INSERT/UPDATE trigger but I have realised that is limited to only front and trailing whitespace, is there any other function in Oracle I could use that would remove internal whitespace? Or do I need to write my own function to do this?
我知道我可以通过不让用户在他们的数字中添加任何空格来解决我的问题,但我从个人经验中知道由于客户端的一些愚蠢的格式而导致验证错误是多么令人沮丧。我已经尝试在我的 INSERT/UPDATE 触发器上使用 TRIM 函数,但我意识到它仅限于前面和后面的空格,我可以使用 Oracle 中的任何其他函数来删除内部空格吗?还是我需要编写自己的函数来做到这一点?
Any pointers would be greatly appreciated.
任何指针将不胜感激。
回答by René Nyffenegger
You want to try replace (telno, ' ', '')
.
你想试试replace (telno, ' ', '')
。
回答by Sarfraz Ahmed
a better approach is to use a regular expression to remove all spaces within a string
更好的方法是使用正则表达式删除字符串中的所有空格
SQL> with t as (
select 'AAA BBB CCC' col from dual union
select 'DDDD EEE F' col from dual
)
--
-- actual query:
--
select regexp_replace(col, '[[:space:]]+', chr(32)) col
from t;
COL
上校
AAA BBB CCC DDDD EEE F
AAA BBB CCC DDDD EEE F