Oracle函数-->字符处理

取得字符串长度:

用lengthb()以字节为单位
用length()以字符为单位

截取字符串:substr

oracle中,可以用substr函数截取字符串.

语法:
substr( string, start_position, [ length ] )
取子字符串,从start_position开始,取length个,length为可选,如果length为空则返回start_position后的所有字符。

实例:

    substr('This is a test', 6, 2)          would return 'is'
    substr
('This is a test', 6)              would return 'is a test'
    substr
('TechOnTheNet', 1, 4)     would return 'Tech'
    substr
('TechOnTheNet', -3, 3)    would return 'Net'
    substr
('TechOnTheNet', -6, 3)    would return 'The'
    substr
('TechOnTheNet', -8, 2)    would return 'On'

select substr('TechOnTheNet', -3, 2) from dual
start
_position为负数时,表示从字符串尾巴倒着数。

 

在oracle的数据库里有个函数 LPAD(String a,int length,String addString). 

  作用:把addString添加到a的左边,length 是返回值的长度。 

例子 

   A : SQL> select lpad('test',8,0) from dual; 

         LPAD('TEST',8,0)
                    ----------------
                 0000test 

        B: select lpad('test',8) from dual;

              LPAD('TEST',8)
                         -------------- 
      【    test】 注:不写最后一个参数,函数会默认在返回值左边加一个空格。

               C: SQL> select lpad('test',2,0) from dual;

                LPAD('TEST',2,0)
                ----------------
              te

              D:SQL> select lpad('test',3) from dual;

                   LPAD('TEST',3)
                  --------------
                    tes

语法:LPAD(expr,n[,pad])

RPAD(expr,n[,pad])

e.g.

select lpad('hello',8,'xy') from dual ='xyxhello'

select lpad('我是一个兵',20,'xy') from dual  ='xyxyxyxyxy我是一个兵'

select lpad('hello',4,'xy') from dual ='hell'

即如果n小于字符串的个数(一个汉字等于两个普通字符串),则会取自左到右n个字符。如果大于,则在字符串左边按顺序循环从‘xy’取值,返回字符串的个数是n。

select rpad('hello',8,'xy') from dual ='helloxyx'

select rpad('hello',4,'xy') from dual ='hell'

即如果n小于字符串的值,则两个函数返回值是一样的。

你可能感兴趣的:(oracle,sql,.net)