Oracle 常用函数/语法总结

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'))

9.sign()

sign函数返回一个运算结果, 大于0 返回1, 小于0返回-1,等于0返回0

 select sign(1012030123-12312412) from dual;  -- 结果返回1
案例代码

实际项目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"
  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值