[英]How to remove both leading and trailing spaces within a group of data in Oracle11gR2?
I am looking for a means of removing leading and trailing spaces based on the following scenario within an Oracle 11gR2 DB using SQL. 我正在寻找一种使用SQL在Oracle 11gR2 DB中根据以下方案删除前导空格和尾随空格的方法。 Not sure if I require REGEXP_REPLACE.
不知道我是否需要REGEXP_REPLACE。
At the moment I have the following possible scenario: 目前,我有以下可能的情况:
v_entries := '"QAZ "," QWERTY ","BOB "," MARY ","HARRY"," TOM JONES "'
where you can see that I have both leading as well as trailing spaces within some of data within the double quotes, ie " QWERTY "
but not all like "HARRY"
, which is fine. 在这里,您可以看到在双引号内的某些数据中我既有前导空格也有尾随空格,即
" QWERTY "
但并非都像"HARRY"
,这很好。
What I am after and unsure how to do in Oracle 11gR2 is this result, ie: 我所追求的并不确定如何在Oracle 11gR2中执行此操作,即:
v_entries := '"QAZ","QWERTY","BOB","MARY","HARRY",,"TOM JONES"'
where all leading/trailing spaces have been removed, where they exists within the double quote groupings but only from the beginning and ends of each grouping. 所有前导/后缀空格均已删除的地方,它们存在于双引号分组中,但仅从每个分组的开头和结尾开始。
Groups that have spaces within the actual values, should not be removed, just like my "TOM JONES"
- want to preserve the given name space surname value here. 实际值中包含空格的组不应删除,就像我的
"TOM JONES"
-要在此处保留给定的名称空间姓氏值。
Assuming you don't want to split the string into its components, you can use a regular expression to remove the spaces. 假设您不想将字符串拆分为各个部分,则可以使用正则表达式删除空格。 If you want to get rid of all spaces - so you don't have any two-word values, for example - then you can use:
如果要摆脱所有空格(例如,没有任何两个单词的值),则可以使用:
v_entries := regexp_replace(v_entries, '[[:space:]]', null);
But if you might have spaces in the middle of a value that you want to preserve you can only remove those either side of a double-quote character: 但是,如果您要保留的值中间可能有空格,则只能删除双引号字符的任一侧:
v_entries := '"QAZ "," QWERTY ","BOB "," MARY ","HARRY"';
For example: 例如:
declare
v_entries varchar2(80);
begin
v_entries := '"QAZ "," QWERTY ","BOB "," MARY ","TOM JONES"';
v_entries := regexp_replace(v_entries, '[[:space:]]*"[[:space:]]*', '"');
dbms_output.put_line(v_entries);
end;
/
anonymous block completed
"QAZ","QWERTY","BOB","MARY","TOM JONES"
You should use the trim()
function to remove leading and trailing spaces. 您应该使用
trim()
函数删除前导和尾随空格。 Here's the documentation on how to use trim()
. 这是有关如何使用
trim()
的文档 。
Combining the LRTIM and RTRIM would do the trick 结合使用LRTIM和RTRIM可以解决问题
SELECT RTRIM(LTRIM (' QWERTY ') ) FROM DUAL;
In PLSQL 在PLSQL中
DECLARE
check varchar2(30) := ' QWERTY ';
BEGIN
dbms_output.put_line(RTRIM(check));
dbms_output.put_line(LTRIM(check));
dbms_output.put_line(RTRIM(LTRIM (check)));
END;
/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.