我這里所說(shuō)的是針對(duì)Oracle數(shù)據(jù)庫(kù)特有的函數(shù),不包括SQL本身就有的。幾個(gè)常用的Oracle函數(shù):nvl、to_char、to_date、to_number、trim、length、substr、等等,當(dāng)然我這是根據(jù)自己的使用情況進(jìn)行列舉,如果有遺漏的,還請(qǐng)大家?guī)兔ρa(bǔ)充,給出具體的例子,謝謝。
select nvl(sum(c.f_factplan),0) f_factplan from pay_certificate c left join pay_reporting r on r.f_id=c.f_repid where to_number(r.f_year||trim(to_char(r.f_month,'09')))<(select to_number(r.f_year||trim(to_char(r.f_month,'09'))) yearmonth from pay_certificate t left join pay_reporting r on r.f_id=t.f_repid where t.f_id=7) and c.f_ctrid=15 |
上面這條sql語(yǔ)句綜合運(yùn)用了好幾個(gè)函數(shù),這里,我們不考慮這條SQL語(yǔ)句的業(yè)務(wù),只單純的從SQL出發(fā)。to_char(r.f_month,'09'),將f_month字段中的月份每個(gè)都轉(zhuǎn)換為兩位長(zhǎng)度,這種寫(xiě)法可能是因?yàn)閒_month字段定義成了數(shù)字型,但是在查詢(xún)中,又需要將它與年份組合成一個(gè)形如yyyymm格式的數(shù)字來(lái)進(jìn)行大小的比較,這個(gè)例子就是如此,trim去掉組合后前后空格,然后用||操作符將它與年組合起來(lái)轉(zhuǎn)換為數(shù)字。
select * from dtaq_data d inner join dtaq_data_info i on d.info_id=i.info_id where d.point_id = 1199 and i.info_type='T' and d.write_date <= to_date('2008-11-21 23:59:59','yyyy-mm-dd hh24:mi:ss') |
這條SQL語(yǔ)句主要運(yùn)用的就是to_date函數(shù),記注意這里的小時(shí)、分、秒,與我們平時(shí)的yyyy-MM-dd HH:mm:ss的寫(xiě)法不一樣,我們現(xiàn)在的時(shí)間一般都是使用24小時(shí)制,所以這里要寫(xiě)成hh24,如果是12小時(shí)制就去掉24
select a.f_id,a.f_name from orgaizeation a inner join (select case when length(o.f_nodecode)>8 then substr(o.f_nodecode,1,8) else o.f_nodecode end f_nodecode from orgaizeation o where o.f_id = 23) b on a.f_nodecode=b.f_nodecode order by b.f_nodecode |
這里用到了條件判斷,when...then...else,length函數(shù)判斷字符串的長(zhǎng)度,substr對(duì)字符串進(jìn)行截取,注意起始位置的索引為1,這和我們一般認(rèn)為的0開(kāi)始不一樣。
這幾個(gè)例子都很簡(jiǎn)單,前面提到的幾個(gè)Oracle函數(shù)都有運(yùn)用,我在實(shí)際開(kāi)發(fā)中,這也是用的最多的,其它的還請(qǐng)各位大大分享一下自己的寶貴經(jīng)驗(yàn),互相交流,共同提高。
聯(lián)系客服