SQL Server、Oracle、MySQL和Vertica数据库常用函数对比
Vertica数据库是HP公司新收购的用于BI方面的数据库。
- 绝对值
S:select abs(-1) value
O:select abs(-1) value from dual
M:select abs(-1) value from dual
V:select abs(-1)
- 向上取整
S:select ceiling(-1.001) value
O:select ceil(-1.001) value from dual
M:select ceil(-1.001) value from dual
V:select ceil(-1.001)
- 向下取整
S:select floor(-1.001) value
O:select floor(-1.001) value from dual
M:select floor(-1.001) value from dual
V:select floor(-1.001)
- 取整(截取)
S:select cast(-1.002 as int) value
O:select trunc(-1.002) value from dual
V:select trunc(-1.002)
- 四舍五入
S:select round(1.23456,4) value 1.23460
O:select round(1.23456,4) value from dual 1.2346
M:select round(1.23456,4) value from dual 1.2346
V:select round(1.23456,4.0)
- e为底的幂
S:select Exp(1) value 2.71828182845905
O:select Exp(1) value from dual 2.71828182
M:select Exp(1) value from dual 2.718281828459045
V:select Exp(1.0) 2.71828182845905
- 取e为底的对数
S:select log(2.7182818284590451) value 1
O:select ln(2.7182818284590451) value from dual; 1
M:select ln(2.7182818284590451) value from dual; 1
V: select ln(2.7182818284590451) 1
- 取10为底对数
S:select log10(10) value 1
O:select log(10,10) value from dual; 1
M:select log(10,10) value from dual; 1
M: select log(10,10) 1
- 取平方
S:select SQUARE(4) value 16
O:select power(4,2) value from dual 16
M:select power(4,2) value from dual 16
V:select power(4,2) 16
- 取平方根
S:select SQRT(4) value 2
O:select SQRT(4) value from dual 2
M:select SQRT(4) value from dual 2
V:select SQRT(4) 2
- 求任意数为底的幂
S:select power(3,4) value 81
O:select power(3,4) value from dual 81
M:select power(3,4) value from dual 81
V:select power(3,4) 81
- 取随机数
S:select rand() value
O:select sys.dbms_random.value(0,1) value from dual;
M:select rand() value from dual;
V:select random()
- 取符号
S:select sign(-8) value -1
O:select sign(-8) value from dual -1
M:select sign(-8) value from dual -1
V:select sign(-8) -1
- 圆周率
S:SELECT PI() value 3.14159265358979
O: SELECT ACOS(-1) FROM DUAL;
M: SELECT ACOS(-1) FROM DUAL; 3.141592653589793
V: SELECT PI() 3.14159265358979
数值间比较
- 求集合最大值
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
M:select greatest(1,-2,4,3) value from dual
V:select greatest(1,-2,4,3)
- 求集合最小值
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
M:select least(1,-2,4,3) value from dual
V:select least(1,-2,4,3)
- 如何处理null值(F2中的null以10代替)
Create table tbl (f1 varchar(10),f2 int);
Insert into tbl(f1,f2) values(‘aa’,null);
Insert into tbl(f1,f2) values(‘bb’,7);
S:select F1,IsNull(F2,10) value from Tbl
O:select F1,nvl(F2,10) value from Tbl
M:select F1,ifnull(F2,10) value from Tbl
V:select F1,IsNull(F2,10) value from Tbl
- 求字符ascii码
S:select ascii('a') value
O:select ascii('a') value from dual
M:select ascii('a') value from dual
V:select ascii('a')
- 从ascii码求字符
S:select char(97) value
O:select chr(97) value from dual
M:select char(97) value from dual
V:select chr(97)
- 连接字符串
S:select '11'+'22'+'33' value
O:select CONCAT('11','22') 33 value from dual
M:select concat('11','22','33') value
V:select '11'||'22'||'33'
- 子串位置 --返回3
S:select CHARINDEX('s','sdsq',2) value
O:select INSTR('sdsq','s',2) value from dual
M:select LOCATE('s','sdsq',2) value from dual
V:select INSTR('sdsq','s',2)
- 求子串
S:select substring('abcd',2,2) value
O:select substr('abcd',2,2) value from dual
M:select substr('abcd',2,2) value from dual
V:select substr('abcd',2,2)
- 子串代替 返回aijklmnef
S:SELECT STUFF('abcdef', 2, 3, 'ijklmn') value
O:SELECT Replace('abcdef', 'bcd', 'ijklmn') value from dual
M:SELECT Replace('abcdef', 'bcd', 'ijklmn') value from dual
V:SELECT Replace('abcdef', 'bcd', 'ijklmn')
- 字符串长度
S:len,datalength
O: select length('aaa') value from dual
M: select length('aaa') value from dual
V: select length('aaa '::CHAR(10))
Select length('aaa '::varchar(10))
select length('aaa')
- 大小写转换 lower,upper
- 左补空格(LPAD的第一个参数为空格则同space函数)
S:select space(10)+'abcd' value
O:select LPAD('abcd',14) value from dual
M:select LPAD('abcd',14, ' ') value from dual
V:select LPAD('abcd',14, ' ') value from dual
- 右补空格(RPAD的第一个参数为空格则同space函数)
S:select 'abcd'+space(10) value
O:select RPAD('abcd',14) value from dual
M:select RPAD('abcd',14, ' ') value from dual
V:select RPAD('abcd',14, ' ') value from dual
- 删除空格
S:ltrim,rtrim
O:ltrim,rtrim,trim
M:ltrim,rtrim,trim
V:ltrim,rtrim,trim
日期函数
- 系统时间
S:select getdate() value
O:select sysdate value from dual
M:select now() value from dual
V:select sysdate()
select getdate()
select now() 会显示时区
- 前后几日
S:直接与整数相加减
O:直接与整数相加减
M: select now()+interval 1 day value from dual
V:直接与整数相加减
- 求日期
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
M:select DATE_FORMAT(NOW(),'%Y-%m-%d') value from dual;
V:select to_char(sysdate(),'YYYY-MM-DD')
- 求时间
S:select convert(char(8),getdate(),108) value
O:select to_char(sysdate,'hh24:mm:ss') value from dual
M:select DATE_FORMAT(NOW(),'%T') value from dual
V:select to_char(sysdate,'hh24:mi:ss')
- 取日期时间的其他部分
S:DATEPART 和 DATENAME 函数 (第一个参数决定)
O:to_char函数 第二个参数决定
参数---------------------------------下表需要补充
year yy, yyyy
quarter qq, q (季度)
month mm, m (m O无效)
dayofyear dy, y (O表星期)
day dd, d (d O无效)
week wk, ww (wk O无效)
weekday dw (O不清楚)
Hour hh,hh12,hh24 (hh12,hh24 S无效)
minute mi, n (n O无效)
second ss, s (s O无效)
millisecond ms (O无效)
M:date_format函数 第二个参数决定
格式
描述
%a
缩写星期名
%b
缩写月名
%c
月,数值
%D
带有英文前缀的月中的天
%d
月的天,数值(00-31)
%e
月的天,数值(0-31)
%f
微秒
%H
小时 (00-23)
%h
小时 (01-12)
%I
小时 (01-12)
%i
分钟,数值(00-59)
%j
年的天 (001-366)
%k
小时 (0-23)
%l
小时 (1-12)
%M
月名
%m
月,数值(00-12)
%p
AM 或 PM
%r
时间,12-小时(hh:mm:ss AM 或 PM)
%S
秒(00-59)
%s