OracleMysqlSqlServer函数区别
Oracle/Mysql/SqlServer函数区别 文章分类:数据库 Sql代码
1.类型转换
--Oracle
select to_number('123') from dual; --123; select to_char(33) from dual; --33;
select to_date('2004-11-27','yyyy/mm/dd') from dual;--2004-11-27
--Mysql
select cast('123' as signed integer); --123 select cast(33 as char(2)); --33;
select to_days('2000-01-01'); --730485
--SqlServer
select cast('123' as decimal(30,2)); --123.00 select cast(33 as char(2)); --33;
select convert(varchar(12) , getdate(), 120)
2.四舍五入函数区别
--Oracle
select round(12.86*10)/10 from dual; --12.9
--Mysql
select format(12.89,1); --12.9
--SqlServer
select round(12.89,1); --12.9
3.日期时间函数
--Oracle
select sysdate from dual; --日期时间
--Mysql
select sysdate(); --日期时间
select current_date(); --日期
--SqlServer
select getdate(); --日期时间
select datediff(day,'2010-01-01',cast(getdate() as varchar(10)));--日期相差天数
4.Decode函数
--Oracle
select decode(sign(12),1,1,0,0,-1) from dual;--1
--Mysql/SqlServer
select case when sign(12)=1 then 1 when sign(12)=0 then 0 else -1 end;--1
5.判空函数
--Oracle
select nvl(1,0) from dual; --1
--Mysql
select ifnull(1,0); --1
--SqlServer
select isnull(1,0); --1
6.字符串连接函数
--Oracle
select '1'||'2' from dual; --12 select concat('1','2'); --12
--Mysql
select concat('1','2'); --12
--SqlServer
select '1'+'2'; --12
7.记录限制函数
--Oracle
select 1 from dual where rownum <= 10; -- Oracle 分页算法一 select * from (
select page.*,rownum rn from (select * from help) page
-- 20 = (currentPage-1) * pageSize + pageSize where rownum <= 20 )
-- 10 = (currentPage-1) * pageSize where rn > 10;
-- Oralce 分页算法二
-- 20 = (currentPage-1) * pageSize + pageSize select * from help where rownum<=20 minus
-- 10 = (currentPage-1) * pageSize
select * from help where rownum<=10;
--Mysql
select 1 from dual limit 10;
select * from dual limit 10,20
--SqlServer select top 10 1
8.字符串截取函数
--Oracle
select substr('12345',1,3) from dual;
--Mysql/SqlServer
select substring('12345',1,3);
8.把多行转换成一合并列
--Oracle
select wm_concat(列名) from dual; --多行记录转换成一列之间用,分割
--Mysql/SqlServer
select group_concat(列名);
SQLServer和Oracle的常用函数对比
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