Oracle 常用函式/語法總結
阿新 • • 發佈:2018-12-26
1. case when then
case end構成語法開始和結束
when 條件滿足執行 then 否則執行 else
case
when ba.org is not null and ba.org = 'GD' then
'GD'
else
'OL'
end "ORDER_SOURCE"
2. decode()
decode(表示式,結果1,表示式/結果…)
decode函式第一個為先決條件,其他欄位表示條件表示式或者結果,
select decode(1,2,3,4,5,1,7) r from dual;--返回值是7
decode(1,2,3,4) 返回值是4 沒有滿足的情況取預設值 4
decode(1,1,3,4) 返回值是3
decode(crk.risklevel,
1,
'C1',
2,
'C2',
3,
'C3',
4,
'C4',
5,
'C5',
'C1') "CUSTOMER_RISK_LEVEL"
where bsb.bankid = decode(t.tradechannel, 0, t.bankid, cc.bankid)
3. (+)
(+) 等同於 left join on , 如果沒有(+) 會過濾掉匹配不上的資料, 這裡只過濾掉 t表的,反之加了(+)就是加上 ba 表中沒有的資料.已 t為主,返回t中所有記錄
where t.serialno = ba.serno(+)
and t.custaccount = cc.custaccount(+)
and t.account = c.account
and t.issueid = i.issueid
4. to_date()
按照執行格式轉成日期型別.
to_date(bs.sysvalue, 'yyyymmdd')
5.to_number()
將欄位轉存成數值型別,該函式會剔除欄位值前面所有的0
例如 0000012358
to_number 之後 12358
to_number(rc.bancsno) "CUSTOMER_ID"
6.to_char()
將時間型別欄位按照格式轉成字元型欄位
to_char(t.tradedate, 'YYYYMMDD') "ESTABLISH_DATE"
7.nvl()
判斷欄位值是否為空,是的話去候選欄位,否則返回fp.price
nvl(fp.price, p.offerprice) "NAV"
8.trunc()
TRUNC函式返回以指定元素格式截去一部分的日期值。
trunc(i.recorddate) >= trunc(to_date(bs.sysvalue, 'yyyymmdd'))
案例程式碼
實際專案SQL程式碼
nvl(to_char(case
when t.attr = 0 and f.islockperiodissue != 1 and
t.trancode not in (0, 1, 2) then
null
else
decode(ba.fudate, null, t.tradedate, ba.fudate)
end,
'HHmmss'),
to_char(t.tradedate, 'hhmmss')) "ESTABLISH_TIME"
where t.serialno = ba.serno(+)
and t.custaccount = cc.custaccount(+)
and t.account = c.account
and t.issueid = i.issueid
and i.serialcode = f.serialcode(+)
and bs.syscode = 'sys.date'
and t.account = crk.account(+)
and bsb.bankid = decode(t.tradechannel, 0, t.bankid, cc.bankid)
and not ((t.trancode = 0 and t.state = 0 and
trunc(i.recorddate) >= trunc(to_date(bs.sysvalue, 'yyyymmdd')) and
i.state in (1, 4)) or
(t.trancode = 2 and t.payamtdate is not null and t.state = 0 and
trunc(t.payamtdate) <> trunc(to_date(bs.sysvalue, 'yyyymmdd'))))
decode(t.memo, '', '0000', '9999') "RETURN_CODE"