Oracle經常使用函數

一、decode數據庫

decode(value,if1,then1,if2,then2,if3,then3,...,[default]);函數

若是不匹配,返回default值;若是未定義default值,則返回空值。編碼

使用DECODE函數能夠避免重複掃描相同記錄或重複鏈接相同的表spa

舉例:code

現有一個商品銷售表sale,表結構爲:排序

  month    char(6)      --月份ci

  sell    number(10,2)   --月銷售金額字符串

  現有數據爲:string

  200001  1000table

  200002  1100

  200003  1200

  200004  1300

  200005  1400

  200006  1500

  200007  1600

  200101  1100

  200202  1200

  200301  1300

  想要轉化爲如下結構的數據:

  year   char(4)      --年份

  month1  number(10,2)   --1月銷售金額

  month2  number(10,2)   --2月銷售金額

  month3  number(10,2)   --3月銷售金額

  month4  number(10,2)   --4月銷售金額

  month5  number(10,2)   --5月銷售金額

  month6  number(10,2)   --6月銷售金額

  month7  number(10,2)   --7月銷售金額

  month8  number(10,2)   --8月銷售金額

  month9  number(10,2)   --9月銷售金額

  month10  number(10,2)   --10月銷售金額

  month11  number(10,2)   --11月銷售金額

  month12  number(10,2)   --12月銷售金額

create or replace view
  v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)
  as
  select
  substrb(month,1,4),
   decode(substrb(month,5,2),'01',sell,0),
   decode(substrb(month,5,2),'02',sell,0),
   decode(substrb(month,5,2),'03',sell,0),
   decode(substrb(month,5,2),'04',sell,0),
   decode(substrb(month,5,2),'05',sell,0),
   decode(substrb(month,5,2),'06',sell,0),
   decode(substrb(month,5,2),'07',sell,0),
   decode(substrb(month,5,2),'08',sell,0),
   decode(substrb(month,5,2),'09',sell,0),
   decode(substrb(month,5,2),'10',sell,0),
   decode(substrb(month,5,2),'11',sell,0),
   decode(substrb(month,5,2),'12',sell,0)
  from A_Test;

結果:顯然不符合要求

方法二:

create or replace view
  v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)
  as
  select
  substrb(month,1,4),
   sum(decode(substrb(month,5,2),'01',sell,0)),
   sum(decode(substrb(month,5,2),'02',sell,0)),
   sum(decode(substrb(month,5,2),'03',sell,0)),
   sum(decode(substrb(month,5,2),'04',sell,0)),
   sum(decode(substrb(month,5,2),'05',sell,0)),
   sum(decode(substrb(month,5,2),'06',sell,0)),
   sum(decode(substrb(month,5,2),'07',sell,0)),
   sum(decode(substrb(month,5,2),'08',sell,0)),
   sum(decode(substrb(month,5,2),'09',sell,0)),
   sum(decode(substrb(month,5,2),'10',sell,0)),
   sum(decode(substrb(month,5,2),'11',sell,0)),
   sum(decode(substrb(month,5,2),'12',sell,0))
  from A_Test
  group by substrb(month,1,4);

結果:bingo

舉例2:

id score
1 100
2 95
3 85
4 84
5 70
6 60
id standard
1 優秀
2 優秀
3 優秀
4 良好
5 良好
6 及格
select id, decode(sign(score-85),1,'優秀',0,'優秀',-1, 
decode(sign(score-70),1,'良好',0,'良好',-1, 
decode(sign(score-60),1,'及格',0,'及格',-1,'不及格'))) standard
from A_Test;

 

select id,
case 
when score>=85 then '優秀'
when score>=70 then '良好'
when score>=60 then '合格'
when score<60 then '不及格'
end standard
from A_Test;


二、sign

sign(value),某個值是0、正數仍是負數,分別返回0、一、-1

三、字符函數

1)截取

substr(str, start, length),按字算

substrb,按字節算,一箇中文2個字節,當所取長度爲奇數時,則自動捨棄最後一位字節

substrc:按Unicode編碼
substr2:按UCS2編碼
substr4:按UCS4編碼

同理,字符串查找 instr(str, findStr[, start][, 第幾個匹配]) 與 instrb。

length(str)和lengthb

2)ASCII(X)  --返回字符X的ASCII碼

chr(x) --CHR和ASCII是一對反函數

CONCAT(X,Y) --鏈接字符串X和Y

LOWER(X) --X轉換成小寫

UPPER(X) --X轉換成大寫

LTRIM(X[,TRIM_STR]) --把X的左邊截去trim_str字符串,缺省截去空格

RTRIM(X[,TRIM_STR])

TRIM([TRIM_STR  FROM]X)

REPLACE(X,old,new)

TRANSLATE(string,from_str,to_str)

--TRANSLATE是REPLACE所提供的功能的一個超集.若是from_str比to_str長,那麼在from_str中而不在to_str中而外的字符將從string中被刪除,由於它們沒有相應的替換字符. to_str不能爲空.Oracle把空字符串認爲是NULL,而且若是TRANSLATE中的任何參數爲NULL,那麼結果也是NULL.

RPAD(string1,x[,string2])

--返回在X 字符長度的位置上插入一個string2中的字符的string1。若是string2的長度要比X字符少,就按照須要進行復制。若是string2多於 X字符,則僅string1前面的X各字符被使用。若是沒有指定string2,那麼使用空格進行填充。X是使用顯示長度能夠比字符串的實際長度要長。 RPAD的行爲方式與LPAD很類似,除了它是在右邊而不是在左邊進行填充。

INITCAP(String) --返回字符串的每一個單詞的第一個字母大寫而單詞中的其餘字母小寫的string

NLS_INITCAP

NLS_LOWER

NLS_UPPER

NLSSORT(string[,nlsparams])

--獲得用於排序string的字符串字節.全部的數值都被轉換爲字節字符串,這樣在不一樣數據庫之間就保持了一致性. Nlsparams的做用和NLS_INITCAP中的相同.若是忽略參數,會話使用缺省排序.

四、數字

ABS(X)

CEIL(X)

FLOOR(X)

ROUND(X[,Y]) --X在第Y位四捨五入

TRUNC(X[,Y]) --X在第Y位截斷

MOD(X,Y) --取餘

POWER(X,Y) --X的Y次冪

LOG(X,Y) --X爲底Y的對數

SQRT(X) --X的平方根

五、NVL(X,VALUE)

若是X爲空,返回value,不然返回X

NVL2(x,value1,value2)

若是x非空,返回value1,不然返回value2

六、聚合函數

sum、avg、count、min、max

相關文章
相關標籤/搜索