1,求一段時間內共跨越了多少天(例如求平均日訪問量時要用到)
select to_date(to_char(max(t.logdate), 'yyyy-MM-dd'), 'yyyy-MM-dd') -
????????????????????????????? to_date(to_char(min(t.logdate), 'yyyy-MM-dd'),'yyyy-MM-dd') + 1
???????????????????????? from tb_log t
2,
Add_months(d,n)?當前日期d后推n個月(n可以為負數)
?? 當找不到某月的匹配日時取該月最后一天。例如3-31后推1個月為4-30
3,last_day(d)? 某日期d所在月份的最后一天
4,轉換某日期為'yyyy-m-d'格式
??? select to_char(sysdate, 'yyyy') || '-' || to_number(to_char(sysdate, 'mm')) || '-' ||
?????? to_number(to_char(sysdate, 'dd')) from dual
5,查詢系統時間戳的毫秒值SELECT (SYSDATE - TO_DATE('1970-1-1 8', 'YYYY-MM-DD HH24')) * 86400000 +
?????? TO_NUMBER(TO_CHAR(SYSTIMESTAMP(3), 'FF')) AS MILLIONS
? FROM DUAL
見http://yangtingkun.itpub.net/post/468/244564
6,oracle函數處理字符串時,下標是從1開始的。
7,
INSTR方法的格式為
INSTR(源字符串, 目標字符串, 起始位置, 匹配序號)? (注:
起始位置是從1開始,不能從0開始)
例如:INSTR('CORPORATE FLOOR','OR', 3, 2)中,源字符串為'CORPORATE FLOOR', 目標字符串為'OR',起始位置為3,取第2個匹配項的位置。
默認查找順序為從左到右。當起始位置為負數的時候,從右邊開始查找。
所以SELECT INSTR('CORPORATE FLOOR', 'OR', -1, 1) "Instring" FROM DUAL的顯示結果是
Instring
——————
14
注:INSTR('test','a')得到的是0,而不是-1參考:
http://df41.spaces.live.com/blog/cns!D064C4537B2605A6!317.entry
http://hi.baidu.com/fgfd0/blog/item/7a48d5f9155a0059252df2dc.html
8,to_date時報"格式代碼出現兩次"
?? 原因是"You are using MM twice"
9,substr( string, start_position, [length])? 下標從1開始
substr('This is a test', 6, 2)?? would return 'is'
substr('This is a test', 6)????? would return 'is a test'
substr('TechOnTheNet', 1, 4)???? would return 'Tech'
substr('TechOnTheNet', -3, 3)??? would return 'Net'
substr('TechOnTheNet', -6, 3)??? would return 'The'
substr('TechOnTheNet', -8, 2)??? would return 'On'
10,select to_date(2008-03-14 14:49:37,'yyyy-MM-dd hh24:mi')? from dual
報錯:ora-01830錯誤:日期格式圖片在轉換整個輸入字符串之前結束
原因是格式化的字符竄長度大于格式化標準(yyyy-MM-dd hh24:mi)的長度
注意:月份的mm大小寫不一樣,hh24和hh不一樣,分鐘用mi而不是mm
11,lengthb查看字節數
12,case的用法
select case when t.classtype = 1 then
????????? '名稱一'
???????? when t.classtype = 2 then
????????? '名城二'
?????? end name from tb_test t
13,
where?? t.fsdate <= to_date('2007-12-15','yyyy-mm-dd')
?where?? to_char(t.fsdate,'yyyy-mm-dd')<='2007-12-15'
14,decode
decode(條件,值1,翻譯值1,值2,翻譯值2,...值n,翻譯值n,缺省值)
15,跨多少月
select to_char(add_months(to_date('1999-2', 'yyyy-mm'), (rownum - 1)), 'YYYY-MM') as TEMPMONTH from dual connect by rownum < months_between(to_date('2009-9', 'yyyy-mm'), to_date('1999-2', 'yyyy-mm')) + 2
16,統計字符串中某個字符出現的次數
SELECT LENGTHB('ABCDEFGEFGDBE')-LENGTHB(REPLACE('ABCDEFGEFGDBE','E','')) FROM DUAL;
http://windows9834.blog.163.com/blog/static/2734500420103154920954/