orcle函數常見用法

orcle常見函數

/*字符函數*/
/*變成大寫*/
select Upper('abcde') from dual;
/*變成小寫*/
select lower('ADCSE') from dual;
/*第一個字母變成大寫*/
select Initcap('dkdkdkdkd') from dual;
/*合併字符*/
select concat('a', 'b')
  from dual;
        select 'a' || 'b'
          from dual;
               /*截取字符串*/
                 select substr('abcde', length('abcde') - 2) from dual;



/*從開始的位置截取要的個數字符串,-代表從後向前計算,+代表從前向後計算*/
select substr('abcde', -5, 3) from dual;
/*計算字符串長度 不區分半角全角*/
select Length(t.membername)
  from MEMBERINFO t
       /*替換*/
         select replace('abcae', 'a', 'm') from dual;


/*查詢匹配字符串所在的位置 相當於 index of*/
select Instr('Hello World', 'or') from dual;
/*左側填充*/
select Lpad('Smith', 10, '*')
  from dual
       /*右側填充*/
         select Rpad('Smith', 10, '*')
           from dual
                /*去除左右空格*/
                  select trim('  dfd  ') from dual;



/*數值函數*/
/*四捨五入*/
select round(415, -1) from dual;
select round(414.21, 1) from dual;

/*取餘*/
select Mod(12, 11)
  from dual;
       /*
       TRUNC(number,num_digits)
       Number 需要截尾取整的數字。
       Num_digits 用於指定取整精度的數字。Num_digits 的默認值爲 0。
       TRUNC()函數截取時不進行四捨五入
       */
         select trunc(123.458) from dual; --123
select trunc(123.458, 0) from dual; --123
select trunc(123.458, 1) from dual; --123.4
select trunc(123.458, -1) from dual; --120
select trunc(123.458, -4) from dual; --0
select trunc(123.458, 4) from dual; --123.458
select trunc(123) from dual; --123
select trunc(123, 1) from dual; --123
select trunc(123, -1) from dual; --120

/*日期函數*/
/*計算月份差*/
select months_between(sysdate, t.createdate)
  from MEMBERINFO t;
       /*增加月份*/
         select add_months(sysdate, 1)
           from dual;
                /*得到下一個星期一的日期*/
                  select next_day(sysdate, '星期一')
                    from dual;
                         /*本月最後一天*/
                           select last_day(sysdate) from dual;




/*轉換函數*/
--獲得當前年
select to_char(sysdate, 'yyyy') from dual;
--獲得當前年月日
select to_char(sysdate, 'fmyyyy-mm-dd') from dual;
--格式化價格樣式
select to_char('11112222', 'L999,999,999') from dual; --¥11,112,222
--返回在星期, 星期日爲1,星期一爲2,星期二爲3.........星期六爲7
select to_char(sysdate, 'D') from dual;

--轉化成數字
select to_number('13') + to_number('14') from dual;

--轉化成日期
select to_date('20090210', 'yyyyMMdd') from dual;

/*通用函數*/
-- 如爲空 返回默認值
select nvl(null, 0)
  from dual;
       -- 如果表達式 exp1 與 exp2 的值相等則返回null,否則返回exp1的值
         select nullif('aaa', 'aaa') from dual;


--如果參數表達式expr1值爲NULL,則NVL2()函數返回參數表達式expr3的值;如果參數表達式expr1值不爲NULL,則NVL2()函數返回參數表達式expr2的值。
select nvl2(null, '11' + '22', '22') total from dual;
--返回表達式中第一個非空表達式
select coalesce(null, 11, 0) from dual;

--case 表達式
select case t.memberstate
         when '10' then
          '初建檔未生效'
         when '20' then
          '待激活狀態'
         when '30' then
          '已激活狀態'
         when '50' then
          '停用'
         when '60' then
          '退費'
         when '70' then
          '刪除'
         else
          '未知狀態'
       end 狀態
  from MEMBERINFO t;

--DECODE 函數 與case 表達式類似 decode() 函數也用於實現多路分支結構
select decode(memberstate,
              '10',
              '初建檔未生效',
              '20',
              '待激活狀態',
              '30',
              '已激活狀態',
              '50',
              '停用',
              '60',
              '退費',
              '70',
              '刪除',
              '未知狀態') 部門
  from MEMBERINFO;

/*分組函數*/
--count 計算總數 沒有數據顯示0 
select count(*) from dual;
--平均值
select avg(memberstate) from MEMBERINFO;
--max 最大值
select max(memberstate) from MEMBERINFO;
--min 最小值
select min(memberstate) from MEMBERINFO;
--sum 求和
select sum(memberstate) from MEMBERINFO;

--Group by 子句 可以與分組函數一起使用
select avg(memberid), memberstate from memberinfo t group by memberstate;

--having 子句
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章