常見字符函數(shù)
select lower('HELLO'),upper('hello'),initcap('HeLlo') from dual;--hello HELLO Hello
select trim('H' from 'HelloHH'),ltrim('HelloHH','H'),rtrim('HelloHH','H') from dual;--ello elloHH Hello
select lpad(1000,7,'*'),rpad(1000,7,'*') from dual;--***1000 1000***
select instr('hello tom','tom')from dual;--7
select substr('hello',3,2)from dual;--ll
select concat('my','tom'),length('tom') from dual;--mytom 3
select replace('hello','h','k') from dual;--kello
常見數(shù)值函數(shù)
四舍五入:
select round(123.456,2),round(123.456,-2),round(123.456,0),round(123.456) from dual;--123.46 100 123 123
去尾:
select trunc(123.456,2),trunc(123.456,-2),trunc(123.456,0),trunc(123.456) from dual;--123.45 100 123 123
求模(取余):
select mod(12,7) from dual;--5
常見日期函數(shù)
sysdate 獲取系統(tǒng)當(dāng)前時(shí)間(數(shù)據(jù)庫)
months_between 兩個(gè)日期間的相差月數(shù)
add_months 獲取幾個(gè)月后的日期
last_day 本月最后一天
extract 日期中某部分值
select sysdate from dual;--2016-11-15 11.22.50
select sysdate+2 from dual;--2016-11-17 11.22.52
select sysdate+12/24 from dual;--2016-11-15 23.24.22
select add_months(sysdate,3) from dual;--2017-02-15 11.25.33
select user_name ,months_between(sysdate,create_date) months1,trunc(months_between(sysdate,create_date))months2 from um_user where user_name='王國勇';--王國勇 3.44782818100358422939068100358422939068 3
select last_day(sysdate) from dual;--2016-11-30 11.31.51
select extract(year from sysdate) from dual;--2016
select extract(month from sysdate) from dual;--11
select extract(day from sysdate) from dual;--15
常見轉(zhuǎn)換函數(shù)
年:year,yyyy,yy;月:month,mon,mm;日:DAY(monday),DY(mon),D(1)
select to_char(sysdate,'YYYY-MM-DD') from dual;--2016-11-15
L:表示本地貨幣符號馋记;$:直接顯示灯荧;9:表示數(shù)值川梅;0:直接顯示偏友,強(qiáng)制占位
select to_char(123.4,'L9,999,999.00') from dual;-- ¥123.40
select to_date('2016-11-11','YYYY-MM-DD') from dual;--2016-11-11 00.00.00
MAX,MIN:用在字符、數(shù)值、日期類;sum,avg:用在數(shù)值類型;count:統(tǒng)計(jì)表達(dá)式值不為空的行數(shù)
select max(create_date),min(create_date),max(group_id),min(group_id) from um_user;--2016-11-10 16.39.01 2004-10-26 09.34.34 902 60
select count(*) from dual;--1
聚合函數(shù)
在select子句中出現(xiàn)的非分組列名眉,必須出現(xiàn)在group by 子句中。
select *|{[distinct]列|表達(dá)式[[as]列別名],...}
from 表名
[where 條件]
[group by 列,...]--根據(jù)指定的列進(jìn)行分組統(tǒng)計(jì)
[having分組條件]--限定顯示分組后的數(shù)據(jù)
[order by 列|列別名|表達(dá)式|序號[asc|desc],...];