Oracle字符串中前后包含空格处理方法

今天碰到个问题,有个列中的字段前后都有不同数量的空格,开发人员要求去掉这些空格。其实Oracle早就替我们准备好trim函数来处理这样的问题。让我们实验一下,学习记住trim这个小东东。
 
二、实验
1.创建测试数据

SAM@OCM11G >create table test_space(id int,name varchar2(10));

Table created.

SAM@OCM11G >insert into test_space values (1,'sam');

1 row created.

SAM@OCM11G >insert into test_space values (2,' sam ');

1 row created.

SAM@OCM11G >insert into test_space values (3,' sam ');

1 row created.

SAM@OCM11G >commit;

Commit complete.

2.查询数据发现问题,并做字符统计

SAM@OCM11G >select * from test_space;


        ID NAME
---------- ----------
        1 sam
        2 sam
        3 sam

SAM@OCM11G >select id,length(name) from test_space;

ID LENGTH(NAME)
---------- ------------
        1 3
        2 5
        3 8

3.使用trim去掉空格

SAM@OCM11G >select id,length(trim(name)) from test_space;


        ID LENGTH(TRIM(NAME))
---------- ------------------
        1 3
        2 3
        3 3

SAM@OCM11G >update test_space set name=trim(name);

3 rows updated.

SAM@OCM11G >select * from test_space;

ID NAME
---------- ----------
        1 sam
        2 sam
        3 sam

4.检查(问题已处理)

SAM@OCM11G >select id,length(name) from test_space;

ID LENGTH(NAME)
---------- ------------
        1 3
        2 3
        3 3

三、总结
    一个小小的trim函数,真是难者不会,会者不难。通过这个小案例,我又多掌握了ORACLE的一个小功能,学习脚步不能停。好久没有发文了,再给自己一针强心剂。加油 Sam!Where there is a will, there is a way.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/2b0c6d4a9e012ff9696fdbf0a19c89d4.html