修剪后不会移除尾随空间

时间:2018-03-28 11:00:55

标签: oracle oracle11g oracle11gr2

我运行此查询:

select trim(utl_raw.cast_to_varchar2((nlssort(d.det_entete, 'nls_sort=binary_ai')))) entete,
length(trim(utl_raw.cast_to_varchar2((nlssort(d.det_entete, 'nls_sort=binary_ai'))))) nb 
from details_param_import d join param_import_table p on d.param_code = p.param_code;

它给出了这个结果:

enter image description here

那么如何删除尾随空格?

1 个答案:

答案 0 :(得分:5)

如果您dump()修改后的值,您可以看到最后有一个空字节,而不是空格;并且length()仍将其视为一个角色:

column entete format a20
column bytes format a40

with details_param_import (det_entete) as (
  select 'nom' from dual
  union all select 'prenom' from dual
)
select trim(utl_raw.cast_to_varchar2((nlssort(d.det_entete, 'nls_sort=binary_ai')))) entete,
length(trim(utl_raw.cast_to_varchar2((nlssort(d.det_entete, 'nls_sort=binary_ai'))))) nb,
  dump(trim(utl_raw.cast_to_varchar2((nlssort(d.det_entete, 'nls_sort=binary_ai'))))) bytes
from details_param_import d
/

ENTETE                       NB BYTES                                   
-------------------- ---------- ----------------------------------------
nom                           4 Typ=1 Len=4: 110,111,109,0              
prenom                        7 Typ=1 Len=7: 112,114,101,110,111,109,0

a side effect of how nlssort() works

您可以明确地将其修剪掉:

with details_param_import (det_entete) as (
  select 'nom' from dual
  union all select 'prenom' from dual
)
select rtrim(utl_raw.cast_to_varchar2((nlssort(d.det_entete, 'nls_sort=binary_ai'))), chr(0)) entete,
length(rtrim(utl_raw.cast_to_varchar2((nlssort(d.det_entete, 'nls_sort=binary_ai'))), chr(0))) nb,
  dump(rtrim(utl_raw.cast_to_varchar2((nlssort(d.det_entete, 'nls_sort=binary_ai'))), chr(0))) bytes
from details_param_import d
/

ENTETE                       NB BYTES                                   
-------------------- ---------- ----------------------------------------
nom                           3 Typ=1 Len=3: 110,111,109                
prenom                        6 Typ=1 Len=6: 112,114,101,110,111,109    

这对你真正想做的事情是否有用是另一回事;查询校对键,而不是用它来进行排序或比较,可能没那么有用。您似乎正在使用它来删除字符串中的重音,但更长的值可能会被截断(as the docs mention),即使对于较短的字符串,它似乎也不能保证您最终会得到您期望的结果。所以,如果有一天破裂,不要太惊讶。