trunc() instr() substr()

来源:互联网 发布:电影特效知乎 编辑:程序博客网 时间:2024/06/05 17:40

 trunc():    截取的意思

    1、截取时间   最终的显示形式都是:年月日时分秒 2016-12-15 00:00:00

    select trunc(sysdate ,'yyyy') from dual ;今年第一天,零点

    select trunc(sysdate ,'yy') from dual ;   今年第一天,零点

    select trunc(sysdate ,'mm') from dual ;这个月第一天,零点

    select trunc(sysdate ,'dd') from dual ;今天,零点

    select trunc(sysdate ) from dual ;今天,零点

    select trunc(sysdate ,'d') from dual ;这个星期第一天,零点

    select trunc(sysdate ,'hh') from dual ;当前时间,精确到小时

    select trunc(sysdate ,'mi') from dual ;当前时间,精确到分钟

    2、截取数字

    trunc(a,b);   a:表示要操作的数     b:表示要保留的小数位数


如下给出一个例子


计算得到相差的天数


 instr():     一般有两种用法:

     1、从一个字符串中查找指定子串的位置

     select instr('abcdeab','a') position from dual; 

     显示结果为1 (说明是从1开始查找a的位置的)

     select instr('abcdeab','a',2) position from dual;

     显示结果为6  从第二个位置开始查找a

     select instr('abcdeab','b',2,2) position from dual;

     显示结果为7  从第二个位置开始查找第二次出现b的位置

     select instr('abcdeab','f') position from dual;

     显示结果为0  找f在字符串中的位置,如果找不到返回结果为0


    2、如下格式时有  like  的作用

    select * from tableA where instr(NAME,'张三')>0;

    等同于

    select * from tableA where name like '%张三%';

   --------------------------------------------------------------    

    select * from tableA where instr(NAME,'张三')=1;

    等同于

    select * from tableA where name like '张三%';

    ---------------------------------------------------------------

    select * from tableA where instr(NAME,'张三')=0;

    等同于

    select * from tableA where name not like '%张三%';

   ---------------------------------------------------------------

   select *  from tableA where instr('张三, 李四', name) > 0; 

   等同于 

   select *  from tableA where name like '%张三%' or name like '%李四%'; 


    3、instr()和like 两者效率问题

    简单的检索两者效率相当,复杂的检索instr()效率比like高,所以可以选择instr()函数来替换like使用



 substr():用来截取字符串

                   select substr('abcdeab',0,2 ) from dual;

         结果为:ab      表示从第一位开始截取2位,此处第一位也是从1开始

         select substr('abcdeab',1,2 ) from dual;

         结果为:ab      表示从第一位开始截取2位,此处第一位也是从1开始

         select substr('abcdeab',3,10 ) from dual;

         结果为:cdeab     从第三位开始截取,如果超出范围,那么到最后一位截止


nvl():判断是否为空

nvl(table.money, 0): 表示如果表中的money字段如果为空,则赋值为0,如果不为空则为本身的值






        
0 0
原创粉丝点击