SQL Server、Oracle、MySQL和Vertica数据库常用函数对比
Vertica数据库是HP公司新收购的用于BI方面的数据库。
- 绝对值
1 2 3 4 5 6 7
| S:select abs(-1) value
O:select abs(-1) value from dual
M:select abs(-1) value from dual
V:select abs(-1)
|
- 向上取整
1 2 3 4 5 6 7
| 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)
|
- 向下取整
1 2 3 4 5 6 7
| 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)
|
- 取整(截取)
1 2 3 4 5
| S:select cast(-1.002 as int) value
O:select trunc(-1.002) value from dual
V:select trunc(-1.002)
|
- 四舍五入
1 2 3 4 5 6 7
| 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为底的幂
1 2 3 4 5 6 7
| 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为底的对数
1 2 3 4 5 6 7
| 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为底对数
1 2 3 4 5 6 7
| 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
|
- 取平方
1 2 3 4 5 6 7
| 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
|
- 取平方根
1 2 3 4 5 6 7
| 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
|
- 求任意数为底的幂
1 2 3 4 5 6 7
| 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
|
- 取随机数
1 2 3 4 5 6 7
| S:select rand() value
O:select sys.dbms_random.value(0,1) value from dual;
M:select rand() value from dual;
V:select random()
|
- 取符号
1 2 3 4 5 6 7
| 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
|
- 圆周率
1 2 3 4 5 6 7
| 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
|
数值间比较
- 求集合最大值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 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)
|
- 求集合最小值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 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代替)
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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码
1 2 3 4 5 6 7
| S:select ascii('a') value
O:select ascii('a') value from dual
M:select ascii('a') value from dual
V:select ascii('a')
|
- 从ascii码求字符
1 2 3 4 5 6 7
| S:select char(97) value
O:select chr(97) value from dual
M:select char(97) value from dual
V:select chr(97)
|
- 连接字符串
1 2 3 4 5 6 7
| 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
1 2 3 4 5 6 7
| 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)
|
- 求子串
1 2 3 4 5 6 7
| 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
1 2 3 4 5 6 7
| 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')
|
- 字符串长度
1 2 3 4 5 6 7 8 9 10 11
| 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函数)
1 2 3 4 5 6 7
| 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函数)
1 2 3 4 5 6 7
| 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
|
- 删除空格
1 2 3 4 5 6 7
| S:ltrim,rtrim
O:ltrim,rtrim,trim
M:ltrim,rtrim,trim
V:ltrim,rtrim,trim
|
日期函数
- 系统时间
1 2 3 4 5 6 7 8 9 10 11
| S:select getdate() value
O:select sysdate value from dual
M:select now() value from dual
V:select sysdate()
select getdate()
select now() 会显示时区
|
- 前后几日
1 2 3 4 5 6 7
| S:直接与整数相加减
O:直接与整数相加减
M: select now()+interval 1 day value from dual
V:直接与整数相加减
|
- 求日期
1 2 3 4 5 6 7 8 9
| 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')
|
- 求时间
1 2 3 4 5 6 7
| 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')
|
- 取日期时间的其他部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| 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无效)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| 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
|
原地址:http://www.verydemo.com/demo_c152_i10134.html