ORACAL函数

本文包括字符函数,数学函数。日期函数,聚合函数,伪列rownum,分页查询

字符函数

select substr('abcdefg',1,5)substr,       --字符串截取
       instr('abcdefg','bc') instr,       --查找子串
       'Hello'||'World' concat,           --连接
       trim('  wish  ') trim,             --去前后空格
       rtrim('wish  ') rtrim,             --去后面空格
       ltrim('  wish') ltrim,             --去前面空格
       trim(leading 'w' from 'wish') deleteprefix,  --去前缀
       trim(trailing 'h' from 'wish') deletetrailing, --去后缀
       trim('w' from 'wish') trim1,
       ascii('A') A1, 
       ascii('a') A2,     --ascii(转换为对应的十进制数)
       chr(65) C1, 
       chr(97) C2,        --chr(十进制转对应字符)
       length('abcdefg') len,   --length 
       lower('WISH')lower, 
       upper('wish')upper, 
       initcap('wish')initcap,     --大小写变换
       replace('wish1','1','youhappy') replace,   --替换
       translate('wish1','1','y')translate,  --转换,对应一位(前面的位数大于等于后面的位数)
       translate('wish1','sh1','hy')translate1,
       concat('11','22') concat   --连接
       wm_concat('列1','列2') --列名合并
from dual; 

select to_number('0123')number1,       --转化字符串到数字
       trunc(to_number('0123.123'),2) number2,
       to_number('120.11','999.99') number3,
       to_number('0a','xx') number4,   --转化十六进制到十进制
       to_number(100000,'xxxxxx') number5
from dual;

数学函数

--绝对值: abs()
   select abs(-2) value from dual;          --(2)
--取整函数(大): ceil()
   select ceil(-2.001) value from dual;       --(-2)
--取整函数(小): floor()
   select floor(-2.001) value from dual;       --(-3)
--取整函数(截取): trunc()
   select trunc(-2.001) value from dual;       -- (-2)
--四舍五入: round()
   select round(1.234564,4) value from dual;       --(1.2346)
--取平方:Power(m,n)
   select power(4,2) value from dual;       --(16)
--取平方根:SQRT()
   select sqrt(16) value from dual;       --(4)
--取随机数: dbms_random(minvalue,maxvalue)
   select dbms_random.value() from dual;  (默认是0到1之间)
 select dbms_random.value(2,4) value from dual;  (2-4之间随机数)
--取符号: Sign()
  select sign(-3) value from dual; --(-1)
  select sign(3) value from dual; --(1)
--取集合的最大值: greatest(value)
   select greatest(-1,3,5,7,9) value from dual;       --(9)
--取集合的最小值: least(value)
   select least(-1,3,5,7,9) value from dual;       --(-1)
--处理Null值: nvl(空值,代替值)
   select nvl(null,10) value from dual;       --(10)
   select nvl(score,10) score from student;

日期函数

--日期
--年 yyyy yyy yy year
--月 month mm mon month
--日+星期  dd ddd(一年中第几天) dy day 
--小时  hh hh24 
--分 mi
--秒 ss

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')currenttime, 
      to_char(sysdate,'yyyy') year,
      to_char(sysdate,'mm') month,
      to_char(sysdate,'dd') day,
      to_char(sysdate,'day') week,
      to_char(sysdate,'hh24')hour,
      to_char(sysdate,'mi') minute,
      to_char(sysdate,'ss') second
from dual;

select to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss')currenttime,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'yyyy')year,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'mm')month,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'dd') day,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'day') week,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'day','NLS_DATE_LANGUAGE=American') week, --设置语言
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'hh24')hour,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'mi') minute,
       to_char(to_date('2009-07-04 05:02:01','yyyy-mm-dd hh24:mi:ss'),'ss') second
from dual;

--months_between
select months_between(to_date('03-31-2014','MM-DD-YYYY'),to_date('12-31-2013','MM-DD-YYYY')) "MONTHS"
 FROM DUAL;

--next_day
select sysdate today, next_day(sysdate,6) nextweek from dual;

--时间区间
select cardid, borrowdate from borrow where to_date(borrowdate,'yyyy-mm-dd hh24:mi:ss')  
between 
to_date('2014-02-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and 
to_date('2014-05-01 00:00:00','yyyy-mm-dd hh24:mi:ss');  

--interval 间隔
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') currenttime,
       to_char(sysdate - interval '7' year,'yyyy-mm-dd hh24:mi:ss') intervalyear,   
       to_char(sysdate - interval '7' month,'yyyy-mm-dd hh24:mi:ss') intervalMonth,   
       to_char(sysdate - interval '7' day,'yyyy-mm-dd hh24:mi:ss') intervalday,   
       to_char(sysdate - interval '7' hour,'yyyy-mm-dd hh24:mi:ss') intervalHour,   
       to_char(sysdate - interval '7' minute,'yyyy-mm-dd hh24:mi:ss') intervalMinute,   
       to_char(sysdate - interval '7' second,'yyyy-mm-dd hh24:mi:ss') intervalSecond  
from dual;  

--add_months 增加月份
select add_months(sysdate,12) newtime from dual;

--extract
select extract(month from sysdate) "This Month",   --当前月
extract(year from add_months(sysdate,36)) " Years" from dual;  --当前年份+3

聚合函数

--count
select count(1) as count from student;--效率最高
select count(*) as count from student;    
select count(distinct score) from student; 

--avg
--distinct|all
select avg(score) score from student;
select avg(distinct score) from student;
select classno,avg(score) score from student group by classno;

--max
--distinct|all
select max(score) from student;
select classno, max(score) score from student group by classno;

--min
--distinct|all
select min(score) from student;
select classno, min(score) score from student group by classno;

--stddev(standard deviation)标准差
select stddev(score) from student;
select classno, stddev(score) score from student group by classno;

--sum
select sum(score) from student;
select classno, sum(score) score from student group by classno;

--median--中位数
select median(score) from student;
select classno, median(score) score from student group by classno;

伪列rownum

--rownum小于某个数时可以直接作为查询条件(注意oracle不支持select top)
select * from student where rownum <3;

--查询rownum大于某个数值,需要使用子查询,并且rownum需要有别名
select * from(
    select rownum rn ,id,name from student
    ) 
where rn>2;

    select rownum rn, student.* from student
where rn >3;

--区间查询
select * from (
    select rownum rn, student.* from student) 
where rn >3 and rn<6;

--排序+前n条
select * from (
    select rownum rn, t.* from ( 
        select d.* from DJDRUVER d order  by drivernumber)t 
 )p where p.rn<10;

--排序+区间查询1
select * from (
    select rownum rn, t.* from ( 
        select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES
        )t 
    )p where p.rn<9 and p.rn>6;

--排序+区间查询2
select * from (
    select rownum rn, t.* from ( 
        select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES
        where rownum<9 
where p.rn>6;--效率远高于方式一

分页查询

--假设每页显示10条

--效率低

select * from (
    select rownum rn, d.* from DJDRIVER d  )p 
where p.rn<=20 and p.rn>=10;

select * from (
    select rownum rn, d.* from DJDRIVER d  )p 
where p.rn between 10 and 20;

--效率高 

select * from (
    select rownum rn, d.* from DJDRIVER d where rownum<=20 )p 
where p.rn>=10;


--排序+区间查询1(效率低)

select * from (
    select rownum rn, t.* from ( 
        select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES
    )t 
)p 
where p.rn<=20 and p.rn>=10;

select * from (
    select rownum rn, t.* from ( 
    )t 
)p 
where p.rn between 10 and 20;

--排序+区间查询2(效率高) 

select * from (
    select rownum rn, t.* from ( 
        select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES
        where rownum<=20 
)p 
where p.rn>=10;

分析函数 --over

分组取前三
SELECT t.*
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY 分组条件 ORDER BY 排序规则 DESC) rn,
             b.*
      FROM 表名 b) t
WHERE t.rn <= 3;
select d.* ,Rank() Over(partition by d.分组条件 order by d.排序规则 desc) mm from 表名 d;
# oracal 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×