1. 程式人生 > >plsql和tsql常用函式比較

plsql和tsql常用函式比較

數學函式  1.絕對值  S:select abs(-1) value  O:select abs(-1) value from dual 

2.取整(大)  S:select ceiling(-1.001) value  O:select ceil(-1.001) value from dual 

3.取整(小)  S:select floor(-1.001) value  O:select floor(-1.001) value from dual 

4.取整(擷取)  S:select cast(-1.002 as int) value  O:select trunc(-1.002) value from dual 

5.四捨五入  S:select round(1.23456,4) value 1.23460  O:select round(1.23456,4) value from dual 1.2346 

6.e為底的冪  S:select Exp(1) value 2.7182818284590451  O:select Exp(1) value from dual 2.71828182 

7.取e為底的對數  S:select log(2.7182818284590451) value 1  O:select ln(2.7182818284590451) value from dual; 1 

8.取10為底對數  S:select log10(10) value 1  O:select log(10,10) value from dual; 1 

9.取平方  S:select SQUARE(4) value 16  O:select power(4,2) value from dual 16 

10.取平方根  S:select SQRT(4) value 2  O:select SQRT(4) value from dual 2 

11.求任意數為底的冪  S:select power(3,4) value 81  O:select power(3,4) value from dual 81 

12.取隨機數  S:select rand() value  O:select sys.dbms_random.value(0,1) value from dual; 

13.取符號  S:select sign(-8) value -1  O:select sign(-8) value from dual -1 

14.圓周率  S:SELECT PI() value 3.1415926535897931  O:不知道 

15.sin,cos,tan 引數都以弧度為單位  例如:select sin(PI()/2) value 得到1(SQLServer) 

16.Asin,Acos,Atan,Atan2 返回弧度 

17.弧度角度互換(SQLServer,Oracle不知道)  DEGREES:弧度-〉角度  RADIANS:角度-〉弧度 

數值間比較 

18. 求集合最大值  S:select max(value) value from  (select 1 value  union  select -2 value  union  select 4 value  union  select 3 value)a 

O:select greatest(1,-2,4,3) value from dual 

19. 求集合最小值  S:select min(value) value from  (select 1 value  union  select -2 value  union  select 4 value  union  select 3 value)a 

O:select least(1,-2,4,3) value from dual 

20.如何處理null值(F2中的null以10代替)  S:select F1,IsNull(F2,10) value from Tbl  O:select F1,nvl(F2,10) value from Tbl 

21.求字元序號  S:select ascii( ‘a ‘) value  O:select ascii( ‘a ‘) value from dual 

22.從序號求字元  S:select char(97) value  O:select chr(97) value from dual 

23.連線  S:select ‘11 ‘+ ‘22 ‘+ ‘33 ‘ value  O:select CONCAT( ‘11 ‘, ‘22 ‘)  33 value from dual 

23.子串位置 –返回3  S:select CHARINDEX( ’s ‘, ’sdsq ‘,2) value  O:select INSTR( ’sdsq ‘, ’s ‘,2) value from dual 

23.模糊子串的位置 –返回2,引數去掉中間%則返回7  S:select patindex( ‘%d%q% ‘, ’sdsfasdqe ‘) value  O:oracle沒發現,但是instr可以通過第四個引數控制出現次數  select INSTR( ’sdsfasdqe ‘, ’sd ‘,1,2) value from dual 返回6 

24.求子串  S:select substring( ‘abcd ‘,2,2) value  O:select substr( ‘abcd ‘,2,2) value from dual 

25.子串代替 返回aijklmnef  S:SELECT STUFF( ‘abcdef ‘, 2, 3, ‘ijklmn ‘) value  O:SELECT Replace( ‘abcdef ‘, ‘bcd ‘, ‘ijklmn ‘) value from dual 

26.子串全部替換  S:沒發現  O:select Translate( ‘fasdbfasegas ‘, ‘fa ‘, ‘我 ‘ ) value from dual 

27.長度  S:len,datalength  O:length 

28.大小寫轉換 lower,upper 

29.單詞首字母大寫  S:沒發現  O:select INITCAP( ‘abcd dsaf df ‘) value from dual 

30.左補空格(LPAD的第一個引數為空格則同space函式)  S:select space(10)+ ‘abcd ‘ value  O:select LPAD( ‘abcd ‘,14) value from dual 

31.右補空格(RPAD的第一個引數為空格則同space函式)  S:select ‘abcd ‘+space(10) value  O:select RPAD( ‘abcd ‘,14) value from dual 

32.刪除空格  S:ltrim,rtrim  O:ltrim,rtrim,trim 

33. 重複字串  S:select REPLICATE( ‘abcd ‘,2) value  O:沒發現 

34.發音相似性比較(這兩個單詞返回值一樣,發音相同)  S:SELECT SOUNDEX ( ‘Smith ‘), SOUNDEX ( ‘Smythe ‘)  O:SELECT SOUNDEX ( ‘Smith ‘), SOUNDEX ( ‘Smythe ‘) from dual  SQLServer中用SELECT DIFFERENCE( ‘Smithers ‘, ‘Smythers ‘) 比較soundex的差  返回0-4,4為同音,1最高 

日期函式  35.系統時間  S:select getdate() value  O:select sysdate value from dual 

36.前後幾日  直接與整數相加減 

37.求日期  S:select convert(char(10),getdate(),20) value  O:select trunc(sysdate) value from dual  select to_char(sysdate, ‘yyyy-mm-dd ‘) value from dual 

38.求時間  S:select convert(char(8),getdate(),108) value  O:select to_char(sysdate, ‘hh24:mm:ss ‘) value from dual 

39.取日期時間的其他部分  S:DATEPART 和 DATENAME 函式 (第一個引數決定)  O:to_char函式 第二個引數決定 

40.當月最後一天  s:不知道  o:select last_day(sysdate) value from dual 

41.本星期的某一天(比如星期日)  s:不知道  o:select next_day(sysdate,7) vaule from dual; 

42.字串轉時間  s:可以直接轉或者select cast('2004-09-08' as datetime) value  o:select to_date('2004-01-05 22:09:38', 'yyyy-mm-dd hh24-mi-ss') vaule from dual; 

43.求兩日期某一部分的差(比如秒)  s:select datediff(ss,getdate(),getdate()+12.3) value  o:直接用兩個日期相減(比如d1-d2=12.3)  select (d1-d2)*24*60*60 vaule from dual; 

44.根據差值求新的日期(比如分鐘)  s:select dateadd(mi,8,getdate()) value  o:select sysdate+8/60/24 vaule from dual; 

45.求不同時區時間  s:不知道  o:select new_time(sysdate, ‘ydt ‘, ‘gmt ‘ ) vaule from dual; 

—–時區引數,北京在東8區應該是ydt——-  ast adt 大西洋標準時間  bst bdt 白令海標準時間  cst cdt 中部標準時間  est edt 東部標準時間  gmt 格林尼治標準時間  hst hdt 阿拉斯加?夏威夷標準時間  mst mdt 山區標準時間  nst 紐芬蘭標準時間  pst pdt 太平洋標準時間  yst ydt yukon標準時間 

oracle支援的字元函式和它們的microsoft sql server等價函式: 

函式 oracle microsoft sql server  把字元轉換為ascii ascii ascii  字串連線 concat (expression + expression)  把ascii轉換為字元 chr char  返回字串中的開始字元(左起) instr charindex  把字元轉換為小寫 lower lower  把字元轉換為大寫 upper upper  填充字串的左邊 lpad n/a  清除開始的空白 ltrim ltrim  清除尾部的空白 rtrim rtrim  字串中的起始模式(pattern) instr patindex  多次重複字串 rpad replicate  字串的語音表示 soundex soundex  重複空格的字串 rpad space  從數字資料轉換為字元資料 to_char str  子串 substr substring  替換字元 replace stuff  將字串中的每個詞首字母大寫 initcap n/a  翻譯字串 translate n/a  字串長度 length datelength or len  列表中最大的字串 greatest n/a  列表中最小的字串 least n/a  如果為null則轉換字串 nvl isnull 

日期函式 

函式 oracle microsoft sql server  日期相加 (date column +/- value) or  add_months dateadd  兩個日期的差 (date column +/- value) or  months_between datediff  當前日期和時間 sysdate getdate()  一個月的最後一天 last_day n/a  時區轉換 new_time n/a  日期後的第一個週日 next_day n/a  代表日期的字串 to_char datename  代表日期的整數 to_number  (to_char)) datepart  日期舍入 round convert  日期截斷 trunc convert  字串轉換為日期 to_date convert  如果為null則轉換日期 nvl isnull 

轉換函式 

函式 oracle microsoft sql server  數字轉換為字元 to_char convert  字元轉換為數字 to_number convert  日期轉換為字元 to_char convert  字元轉換為日期 to_date convert  16進位制轉換為2進位制 hex_to_raw convert  2進位制轉換為16進位制 raw_to_hex convert 

其它行級別的函式 

函式 oracle microsoft sql server  返回第一個非空表示式 decode coalesce  當前序列值 currval n/a  下一個序列值 nextval n/a  如果exp1 = exp2, 返回null decode nullif  使用者登入賬號id數字 uid suser_id  使用者登入名 user suser_name  使用者資料庫id數字 uid user_id  使用者資料庫名 user user_name  當前使用者 current_user current_user  使用者環境(audit trail) userenv n/a  在connect by子句中的級別 level n/a 

合計函式 

函式 oracle microsoft sql server  average avg avg  count count count  maximum max max  minimum min min  standard deviation stddev stdev or stdevp  summation sum sum  variance variance var or varp 

oracle還有一個有用的函式extract,提取並且返回日期時間或時間間隔表示式中特定的時間域:  extract(year from 日期)