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

一、问题描述

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.

猜你喜欢

转载自www.linuxidc.com/Linux/2016-11/137684.htm
今日推荐