ORACLE 常用 函式

wzhalal發表於2013-09-27

一)日期函式[重點掌握前四個日期函式]

1,add_months[返回日期加()指定月份後()的日期]

select sysdate S1,add_months(sysdate,10) S2,

add_months(sysdate,5) S3 from dual;

2,last_day [返回該月最後一天的日期]

select last_day(sysdate) from dual;

3,months_between[返回日期之間的月份數]

select sysdate S1, months_between('1-4-04',sysdate) S2,

months_between('1-4-04','1-2-04') S3 from dual

4,next_day(d,day):返回下個星期的日期,day1-7或星期日-星期六,1表示星期日

select sysdate S1,next_day(sysdate,1) S2,

next_day(sysdate,'星期日') S3 FROM DUAL

5,round[舍入到最接近的日期](day:舍入到最接近的星期日)

select sysdate S1,

round(sysdate) S2 ,

round(sysdate,'year') YEAR,

round(sysdate,'month') MONTH ,

round(sysdate,'day') DAY from dual

6,trunc[截斷到最接近的日期]

select sysdate S1,

trunc(sysdate) S2,

trunc(sysdate,'year') YEAR,

trunc(sysdate,'month') MONTH ,

trunc(sysdate,'day') DAY from dual

7,返回日期列表中最晚日期

select greatest('01-1-04','04-1-04','10-2-04') from dual

 

 

二)字元函式(可用於字面字元或資料庫)

1,字串擷取

select substr('abcdef',1,3) from dual

2,查詢子串位置

select instr('abcfdgfdhd','fd') from dual

3,字串連線

select 'HELLO'||'hello world' from dual;

4, 1)去掉字串中的空格

select ltrim(' abc') s1,

rtrim('zhang ') s2,

trim(' zhang ') s3 from dual

2)去掉前導和字尾

select trim(leading 9 from 9998767999) s1,

trim(trailing 9 from 9998767999) s2,

trim(9 from 9998767999) s3 from dual;

5,返回字串首字母的Ascii

select ascii('a') from dual

6,返回ascii值對應的字母

select chr(97) from dual

7,計算字串長度

select length('abcdef') from dual

8,initcap(首字母變大寫),lower(變小寫),upper(變大寫)

select lower('ABC') s1,

upper('def') s2,

initcap('efg') s3 from dual;

9,Replace

select replace('abc','b','xy') from dual;

10,translate

select translate('abc','b','xx') from dual; -- x1

11,lpad [左添充] rpad [右填充](用於控制輸出格式)

select lpad('func',15,'=') s1, rpad('func',15,'-') s2 from dual;

select lpad(dname,14,'=') from dept;

12, decode[實現if ..then邏輯]

select deptno,decode(deptno,10,'1',20,'2',30,'3','其他') from dept;

 

三)數字函式

1,取整函式(ceil向上取整,floor向下取整)

select ceil(66.6) N1,floor(66.6) N2 from dual;

2,取冪(power)求平方根(sqrt)

select power(3,2) N1,sqrt(9) N2 from dual;

3,求餘

select mod(9,5) from dual;

4,返回固定小數位數(round:四捨五入,trunc:直接截斷)

select round(66.667,2) N1,trunc(66.667,2) N2 from dual;

5,返回值的符號(正數返回為1,負數為-1)

select sign(-32),sign(293) from dual;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28894640/viewspace-773464/,如需轉載,請註明出處,否則將追究法律責任。

相關文章