PL/SQL常用函數

一、常用字符函數

1、instr(string1, string2 [, start_position [,  nth_appearance]])

     解釋:string1-源字符串,要在此字符串中查找;string2-要查找的字符串;start_position-代表從string1的那個位置開始查找,如果省略,默認爲1。如果此參數爲正,從左到右開始檢索。如果此參數爲負,從右到左檢索。返回要查找的字符串在源字符創中的開始索引;nth_appearance代表要查找第幾次出現的string2,如果省略,默認爲1.如果爲負數,系統會報錯。如果string2在string1中沒有找到,instr函數返回0.

     舉例:select instr('abc', a) from dual;返回1

                  select instr('abc abc','a', 1, 2)  from dual。返回5

2、substr(char,start,length)

      解釋返回字符串表達式char中從第start開始的length個字符

      舉例:substr('This is a test', 62)     would return 'is'
                substr('This is a test', 6)         would return 'is a test'
                substr('TechOnTheNet', -33)     would return 'Net'
                
substr('abcde',-6) = null
                   substr('abcde',-5) = 'abcde'
                   substr('abcde',-2) = 'de'
                   substr('abcde',-1) = 'e'
                   substr('abcde',-0) = 'abcde'

 

3、trim()

     解釋刪除字符串兩邊的空格

      舉例:trim('   tech   ')  would return 'tech';

 

4、trim([leading | trailing | both] trim_char from string)

     解釋從字符串String中刪除指定的字符trim_charleading:從字符串的頭開始刪除。 trailing:從字符串的尾部開始刪除。borth:從字符串的兩邊刪除。 tim()只能刪除半角空格。 

     舉例:trim(' '  from  '   tech   ')  would return 'tech';

                  trim(leading '0' from '000123')      would return '123';

                  trim(trailing '1' from 'Tech1') would return 'Tech';

                  trim(both '1' from '123Tech111')    would return '23Tech'

5、replace(char, search_string[, replacement_string])

      解釋每個search_string, 都被replacement_string代替。如果replacement_string爲空,那麼所有search_string都被移除。如果search_string空,那麼返回原來的char

      舉例:select replace('abcd', 'cd', 'ef') from dual;    aefd

                 select replace('acdd','cd','')  from dual;        ad

                 select replace('acdd','','ef')  from dual;         acdd

                 select replace('acdd', '', '')  from dual;          acdd

 6、round( number, decimal_places )

      解釋用來對數據進行四捨五入,number --需四捨五入處理的數值,decimal_places --四捨五入 , 小數取幾位 ( 預設爲 0 )返回值類型數字例子

      舉例:select round(123.456, 0) from dual; 回傳 123 
                 select round(123.456, 1) from dual; 回傳 123.5 
                 select round(123.456, 2) from dual; 回傳 123.46 

 

7、 round( date, [ format ] )

      解釋用來對日期進行四捨五入,the round function returns a date rounded to a specific unit of measure;date is the date to round;format is the unit of measure to apply for rounding. If the format parameter is omitted, the round function will round to the nearest day.

      舉例:  

 round(to_date ('22-AUG-03'),'YEAR') would return '01-JAN-04'
round(to_date ('22-AUG-03'),'Q') would return '01-OCT-03'
round(to_date ('22-AUG-03'),'MONTH') would return '01-SEP-03'
round(to_date ('22-AUG-03'),'DDD') would return '22-AUG-03'
round(to_date ('22-AUG-03'),'DAY') would return '24-AUG-03'


 

Below are the valid format parameters:

     
Year SYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y Rounds up on July 1st
ISO Year IYYY, IY, I  
Quarter Q Rounds up on the 16th day of the second month of the quarter
Month MONTH, MON, MM, RM Rounds up on the 16th day of the month
Week WW Same day of the week as the first day of the year
IW IW Same day of the week as the first day of the ISO year
W W Same day of the week as the first day of the month
Day DDD, DD, J  
Start day of the week DAY, DY, D  
Hour HH, HH12, HH24  
Minute MI  

 

 

二、常用日期函數

1、trunc(day, [fmt])

      解釋:按照fmt指定格式對日期數據day做舍入處理,默認截斷到日

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章