zoukankan      html  css  js  c++  java
  • Oracle 函数的简介

    1) concat只能连接两个字符串,||可以连接多个字符串

    select concat('1', '2') from dual;
    select '1'||'2'||'3' from dual;

    2) ceil(n) :取大于等于数值n的最小整数;

    floor(n):取小于等于数值n的最大整数;
    -- 10
    select ceil(9.3) from dual;
    -- 9
    select floor(9.3) from dual;
    -- 9
    select ceil(-9.3) from dual;
    -- 10
    select floor(-9.3) from dual;

    3) 表达式:round(number, [decimals])

    含义:number表示待处理的数字,decimals表示处理的位数(需要四舍五入)

    select round(1234.5678,3) from dual;
    -- 1234.568 小数点后三位
    
    select round(1234.5678, -1) from dual;
    -- 1230 小数点前一位

    4) 表达式:trunc(param, [fmt])

    含义:将param字段的值按照fmt规则截取

    1,日期

    select trunc(sysdate, 'yyyy') from dual --当年的第一天
    select trunc(sysdate, 'mm') from dual --当月的第一天
    select trunc(sysdate, 'dd') from dual --当前时间(精确到天)
    select trunc(sysdate, 'd') from dual --当前星期的第一天
    select trunc(sysdate, 'hh') from dual --当前时间(精确到小时)
    select trunc(sysdate,' mi') from dual --当前时间(精确到分钟,没有精确到秒的)

     2,数字

    select trunc(123.458, 1) from dual;
    -- 123.4 小数点后面一位,不会四舍五入
    
    select trunc(123.458,-1) from dual; 
    -- 120 小数点前面一位

    5) 表达式:decode(条件, 值1, 翻译值1, 值2, 翻译值2, …值n, 翻译值n, 缺省值)

    含义:条件满足值1则返回翻译值1,满足值2则返回翻译值2。。。都不满足,返回缺省值

    select id, decode(gender, 1, '男生', 2, '女生', '其他') gender_mean from member

    6) lpad与rpad

    1,lpad:从左边开始,对字符串使用指定的字符进行填充

    2,rpad:从右边开始

    3,补足位数:比如需要返回结果是两位数,则1返回为01,11返回为11

    lpad( string, padded_length, [ pad_string ] )

    注:rpad和lpad只有方向上的不同

    string:待处理的字符串

    padded_length:返回的字段长度

    [ pad_string ]:可选参数;填充的字符 

    select lpad('abcde',10,'x') from dual;
    -- xxxxxabcde
    
    select lpad('abcde',10,'oq') from dual;
    -- oqoqoabcde
    
    select lpad('abcde',2) from dual;
    -- ab

     7) upper与lower

    select upper(name) from user;
    -- upper:全部转换成大写 
    
    select lower(name) from user; 
    -- lower:全部转换成小写 

    8) instr与substr

    1,instr()

    语法:instr(sourceString,destString,start,appearPosition) → instr('源字符串' , '目标字符串' ,'开始位置','第几次出现');start,appearPosition默认为1

    返回值:查找到的字符串的位置

    注:位置从1开始计算;返回值为指定字符的第一个字符位置,如果start大于第一个字符位置,取第二个字符位置,以此类推

    select instr('yuechaotianyuechao','ao') position from dual;
    -- 得到6

    2,substr()

    语法:substr( string, start_position, [ length ] ) → substr('目标字符串',开始位置,长度)

    注:位置从1开始计算;length不填默认为到末尾;start_position为负数代表从右往左

    substr('This is a test', 6, 2) 
    -- 得到 is

    9) to_date与to_char

    1,to_date()

    select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual;
    -- sysdate是当前系统时间

    select 1, TO_DATE(null) from dual;  //时间为null的用法, 注意要用TO_DATE(null) 

    2,to_char()

    select to_date('2004-05-07 13:23:44', 'yyyy-mm-dd hh24:mi:ss') from dual
    select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;   --日期转化为字符串  
    select to_char(sysdate,'yyyy')  as nowYear   from dual;   --获取时间的年  
    select to_char(sysdate,'mm')    as nowMonth  from dual;   --获取时间的月  
    select to_char(sysdate,'dd')    as nowDay    from dual;   --获取时间的日  
    select to_char(sysdate,'hh24')  as nowHour   from dual;   --获取时间的时  
    select to_char(sysdate,'mi')    as nowMinute from dual;   --获取时间的分  
    select to_char(sysdate,'ss')    as nowSecond from dual;   --获取时间的秒

    10) Order by 

    Oracle默认null是最大值

    select * from table order by id asc nulls first
    --null值始终排前面
    
    select * from table order by id desc nulls last
    --null值始终排后面
  • 相关阅读:
    hdu 5723 Abandoned country 最小生成树 期望
    OpenJ_POJ C16G Challenge Your Template 迪杰斯特拉
    OpenJ_POJ C16D Extracurricular Sports 打表找规律
    OpenJ_POJ C16B Robot Game 打表找规律
    CCCC 成都信息工程大学游记
    UVALive 6893 The Big Painting hash
    UVALive 6889 City Park 并查集
    UVALive 6888 Ricochet Robots bfs
    UVALive 6886 Golf Bot FFT
    UVALive 6885 Flowery Trails 最短路
  • 原文地址:https://www.cnblogs.com/lgx5/p/12181018.html
Copyright © 2011-2022 走看看