sql 函數

表:create table T_Person (FidNumber varchar(20),Fname varchar(20),FbrithDay datetime,
    FregDay datetime,Fweight decimal(10,2));mysql

 

求絕對值:select Fweight-50,abs(Fweight-50),abs(-5.03) from t_person;sql

求冪函數:select Fweight,power(Fweight,-0.5),power(Fweight,2) from t_person;第二個參數爲冪。oracle

求平方根:select fweight,sqrt(fweight) from t_person;dom

求隨機:mysql:select rand();ide

sqlserver:select rand(1234);函數

oracle:select dbms_random.value(10,50) from dual;>=10,<50;sqlserver

select dbms_random.normal from dual;orm

select dbms_random.string(opt,len) from dual;server

DB2:select SYSFUN.rand() from SYSIBM.SYSDUMMY1;ci

舍入最大值:select Fname,fweight,ceiling(fweight),ceil(fweight *-1) from t_person;

舍入最小值:select Fname,fweight,floor(fweight),floor(fweight *-1)from t_person;

四捨五入:select Fname,fweight,round(fweight,1),round(fweight *-1,0),round(fweight,-1) from t_person;後面一位是精度,可爲負。

弧度轉換爲角度:mysql,server:select fname ,fweight,degrees(fweight) from t_person;

Oracle,db2:select fname ,fweight,(Fweight*180)/acos(-1) from t_person;
角度轉弧度:select fname ,fweight,(Fweight*acos(-1))/180 from t_person;     select fname ,fweight,radians(fweight) from t_person;

求符號:select fname ,fweight,sign(Fweight) from t_person;大於0得1,<0=-1,=0=0;

 求發音差別:select soundex('jack'),soundex('jeck') from dual;
select fname,soundex(fname) from t_person;
db2,server有函數(1到4)來判斷髮音區別:select difference (fname,'merry') from t_person;

mysql求時間:select now(),sysdate(),current_date(),current_timestamp(),current_time();

server:求時間:select convert(varchar(50),getdate(),101) as riqi;
select convert(varchar(50),getdate(),108) as sj;

oracle:select to_char(sysdate(),'YYYYMMDD') from dual;

db2:select current timestamp/date/time from sysibm.sysdummyl;

日期加法:mysql:select fbrithday,date_add(fbrithday,interval 1 week) as wk,
date_add(fbrithday,interval 2 month) as mon,
date_add(fbrithday,interval 5 quarter) as week from t_person;

 server:dateadd(datepart,number,date);

Oracle:用+,-計算時間天數,addmonths(date,number)計算月

db2:date+length   unit(單位:day,month,year)

類型轉換:mysql:select cast('-30' as signed) as sig,
          convert('36',unsigned) as usig;

sqlserver:convert(類型,轉換值)與上面相反;Oracle與db2有專門類型轉換函數。o:to_date(exp,format),to_char(exp,format)...;2:date(),int()。

 

空值轉換:select fbrithday,fregday, coalesce(fbrithday,fregday,'2018-08-08') from t_person;從第一個參數開始判斷是否爲空,不爲空則賦這個值。

簡化版:M:IFNULL(EXP,VALUE),S:ISNULL(E,V),O:NVL(E,V);

 

相關文章
相關標籤/搜索