6

几个开源数据库和插件对 oracle 字段类型和函数的兼容情况 -m6米乐安卓版下载

原创 张玉龙 2022-04-03
1365

备注:

  • 对标 oracle 的版本:11.2.0.4
  • 为了美化表格显示,将以下数据库名称进行简写
    • postgresql --> pgsql
    • opengauss --> og
  • 本文仅供参考
表名 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
dual n y n n y y
类型 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
rownum n n y y n n
  • 测试示例:
select * from emp where rownum <= 8;
  • postgresql 替代 rownum 的方式
-- limit postgres=# select * from emp limit 8; -- 窗口函数 row_number() over () select * from (select row_number() over () rownum, * from emp) b where rownum <= 8;

备注:这里的支持理解为能否在数据库中直接创建带有以下类型的表,而并非转换其他类型。

字段 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
char y y y y y y
varchar2 n y y y n y
nvarchar2 n y y y n y
number n n y y n y
float y y y y y y
date 只存储日期 y y y 只存储日期 y
timestamp(6) y y y y y y
blob n n y y y n
clob n n y y n n
nclob n n n n n n
long n n n n y n
long raw n n n n n n
raw n n y y n n

以下仅对oracle的函数和相关数据库做了简单查询使用的对比,针对复杂情况请自行测试,本文仅供参考。

字符函数

函数 说明
ascii(x) 返回字符x的ascii码
concat(x,y) 连接字符串x和y
instr(x,str[,start][,n]) 从x中查找str,可以指定从start开始,也可以指定从n开始
length(x) 返回x的长度(字符个数)
lengthb(x) 返回x的长度(字节数)
lower(x) x转换成小写
upper(x) x转换成大写
initcap(x) 将x中的第一个字母转换成大写
ltrim(x[,trim_str]) 把x的左边截去trim_str字符串,缺省截去空格
rtrim(x[,trim_str]) 把x的右边截去trim_str字符串,缺省截去空格
trim([trim_str from]x) 把x的两边截去trim_str字符串,缺省截去空格
replace(x,old,new) 在x中查找old,并替换成new
substr(x,start[,length]) 返回x的字串,从start处开始,截取length个字符,缺省length,默认到结尾
substrb(x,start[,length]) -
lpad(x,n,y) 在字符串x的左边补齐空格,得到总长为n个字符的字符串。其中y是可选项,这个参数用于指定左边补齐的字符串
rpad(x,n,y) 在字符串x的右边补齐空格,得到总长为n个字符的字符串。其中y是可选项,这个参数用于指定右边补齐的字符串
- -
chr(n) -
nchr(number) -
nls_initcap(char) -
nls_lower(char) -
nls_upper(char) -
nlssort(char) -
regexp_replace(source_char,pattern,replace_string) -
regexp_substr(source_char,pattern) -
regexp_count(source_char,pattern) -
regexp_instr(source_char,pattern) -
soundex(char) -
translate(expr,from_string,to_string) -

上面各函数在 oracle 中执行的例子

sql> select ascii('a') a from dual; --97 sql> select concat('hello','world') a from dual; --helloworld sql> select instr('hello world','or') a from dual; --8 sql> select length('hello') a from dual; --5 sql> select lengthb('hello') a from dual; --5 sql> select lower('hello') a from dual; --hello sql> select upper('hello') a from dual; --hello sql> select initcap('hello') a from dual; --hello sql> select ltrim('=hello=','=') a from dual; --hello= sql> select rtrim('=hello=','=') a from dual; --=hello sql> select trim('='from'=hello=') a from dual; --hello sql> select replace('abcde','cd','aaa') a from dual; --abaaae sql> select substr('abcde',2,3) a from dual; --bcd sql> select substrb('abcde',2,3) a from dual; --bcd sql> select lpad('helloworld',15,'&') a from dual; --&&&&&helloworld sql> select rpad('helloworld',15,'&') a from dual; --helloworld&&&&& sql> select chr(67)||chr(65)||chr(84) "dog" from dual; --cat sql> select nchr(187) from dual; --> sql> select nls_initcap('ijsland') "a" from dual; --ijsland sql> select nls_lower('ijsland') "a" from dual; --ijsland sql> select nls_upper('ijsland') "a" from dual; --ijsland sql> select nlssort('ijsland') "a" from dual; --696a736c616e6400 sql> select regexp_replace('500 oracle parkway, redwood shores, ca', '( ){2,}', ' ') "regexp_replace" from dual; --500 oracle parkway, redwood shores, ca sql> select regexp_substr('500 oracle parkway, redwood shores, ca', ',[^,] ,') "regexpr_substr" from dual; --, redwood shores, sql> select regexp_count('123123123123', '123', 3, 'i') count from dual; --3 sql> select regexp_instr('500 oracle parkway, redwood shores, ca', '[^ ] ', 1, 6) "regexp_instr" from dual; --37 sql> select soundex('b') from dual; --b000 sql> select translate('sql*plus user''s guide', ' */''', '___') from dual; --sql_plus_users_guide
函数 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
ascii y y y y y y
concat y y y y y y
instr n y y y y y
length y y y y y y
lengthb n y y y n y
lower y y y y y y
upper y y y y y y
initcap y y y y n y
ltrim y y y y n y
rtrim y y y y n y
trim y y y y y y
replace y y y y y y
substr y y y y y y
substrb n y y y n y
lpad y y y y y y
rpad y y y y y y
- - - - - - -
chr y y y y n y
nchr n n n n n n
nls_initcap n n n n n n
nls_lower n n n n n n
nls_upper n n n n n n
nlssort n y n n n y
regexp_replace 结果不一致 y 结果不一致 结果不一致 y y
regexp_substr n y y y y y
regexp_count n y n n n y
regexp_instr n y n n y y
soundex n n n n y n
translate y y y y n y
sql> select trim(' helloworld ') from dual; trim('hell ---------- helloworld mysql> select trim(' helloworld ') from dual; ---------------------------- | trim(' helloworld ') | ---------------------------- | helloworld | ---------------------------- 1 row in set (0.00 sec)

数字函数

函数 说明 示例
abs(x) x的绝对值 abs(-3)=3
cos(x) x的余弦 cos(1)=0.540302306
acos(x) x的反余弦 acos(1)=0
sin(x) x的正弦 sin(1)=0.841470985
asin(x) x的反正弦 asin(1)=1.57079633
tan(x) x的正切 tan(1)=1.55740772
atan(x) x的反切 atan(1)=0.785398163
ceil(x) 大于或等于x的最小值 ceil(5.4)=6
floor(x) 小于或等于x的最大值 floor(5.8)=5
log(x,y) x为底y的对数 log(2,4)=2
mod(x,y) x除以y的余数 mod(8,3)=2
power(x,y) x的y次幂 power(2,3)=8
round(x[,y]) x在第y位四舍五入 round(3.456,2)=3.46
sqrt(x) x的平方根 sqrt(4)=2
trunc(x[,y]) x在第y位截断 trunc(3.456,2)=3.45
- - -
atan2(n1,n2) - atan2(.3, .2)=0.982793723
bitand(expr1,expr2) - bitand(6,3)=2
cosh(n) - cosh(0)=1
exp(n) - exp(4)=54.59815
ln(n) - ln(95)=4.55387689
nanvl(n2,n1) - -
remainder(n2,n1) - -
sign(n) - sign(-15)=-1
sinh(n) - sinh(1)=1.17520119
tanh(n) - tanh(.5)=0.462117157
width_bucket(expr, min_value, max_value, num_buckets) - -
函数 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
abs y y y y y y
cos y y y y y y
acos y y y y y y
sin y y y y y y
asin y y y y y y
tan y y y y y y
atan y y y y y y
ceil y y y y y y
floor y y y y y y
log y y y y y 显式转换
mod y y y y y y
power y y y y y y
round (number) y y y y y y
sqrt y y y y y y
trunc (number) y y y y n y
- - - - - - -
atan2 y y y y y y
bitand n y y y n y
cosh y y n n n y
exp y y y y y y
ln y y y y y y
nanvl n y n n n y
remainder n n n n n n
sign y y y y y y
sinh y y n n n y
tanh y y n n n y
width_bucket y y y y - y

以下情况为什么需要显式转换呢

oracle=# \df log list of functions schema | name | result data type | argument data types | type ------------ ------ ------------------ --------------------- ------ oracle | log | "number" | "number" | func oracle | log | "number" | "number", "number" | func pg_catalog | log | double precision | double precision | func pg_catalog | log | numeric | numeric | func pg_catalog | log | numeric | numeric, numeric | func (5 rows) oracle=# select log(2,4) a from dual; error: function log(integer, integer) is not unique line 1: select log(2,4) a from dual; ^ hint: could not choose a best candidate function. you might need to add explicit type casts. oracle=# show search_path; search_path ---------------------------- public, oracle, pg_catalog (1 row) oracle=# select log(2::number,4) a from dual; a --- 2 (1 row) oracle=# select log(2::numeric,4) a from dual; a -------------------- 2.0000000000000000 (1 row)

nanvl

sql> create table float_point_demo (dec_num number(10,2), bin_double binary_double, bin_float binary_float); table created. sql> insert into float_point_demo values (0,'nan','nan'); 1 row created. sql> select * from float_point_demo; dec_num bin_double bin_float ---------- ---------- ---------- 0 nan nan sql> select bin_float, nanvl(bin_float,0) from float_point_demo; bin_float nanvl(bin_float,0) ---------- ------------------ nan 0

binary_double 和 binary_float 字段类型只有 opengauss 2.1 支持创建

opengauss=# create table float_point_demo (bin_double binary_double, bin_float binary_float); create table opengauss=# \d float_point_demo table "public.float_point_demo" column | type | modifiers | storage | stats target | description ------------ ------------------ ----------- --------- -------------- ------------- bin_double | double precision | | plain | | bin_float | real | | plain | | has oids: no options: orientation=row, compression=no

日期函数

函数 说明
sysdate 获得当前系统时间
add_months(d,n) 在某一个日期 d 上,加上指定的月数 n,返回计算后的新日期。
last_day(d) 返回指定日期当月的最后一天。
months_between(x,y) 给出date(y) - date(x) 的月数(可以是小数)
next_day(x,day) 返回从x开始一周内,下一个day的日期,这里的day为星期
extract(fmt from d) 提取日期中的特定部分。
round(d[,fmt]) 返回一个以 fmt 为格式的四舍五入日期值, d 是日期, fmt 是格式
trunc(d[,fmt]) 返回一个以 fmt 为格式的截断日期值(不进行四舍五入), d 是日期, fmt 是格式
to_char(datetime/interval) 将日期格式转为字符格式
- -
current_date -
current_timestamp -
dbtimezone -
sessiontimezone -
from_tz(timestamp_value,time_zone_value) -
systimestamp -
localtimestamp -
new_time(date,timezone1,timezone2) -
numtodsinterval(n,interval_unit) -
numtoyminterval(n,interval_unit) -
sys_extract_utc(datetime_with_timezone) -
to_dsinterval -
to_timestamp -
to_timestamp_tz -
to_yminterval -
tz_offset -

上面各函数在 oracle 中执行的例子

sql> select sysdate from dual; --2022-04-02 23:10:36 sql> select add_months(sysdate,2) from dual; --2022-06-02 23:10:59 sql> select last_day(sysdate) from dual; --2022-04-30 23:11:25 sql> select months_between(sysdate,to_date('20181120','yyyymmdd')) a from dual; --40.4505373 sql> select next_day(sysdate,'monday') from dual; --2022-04-04 23:12:45 sql> select extract(year from sysdate) "year" from dual; --2022 sql> select round(to_date('20190715','yyyymmdd'),'month') from dual; --2019-07-01 00:00:00 sql> select round(to_date('20190716','yyyymmdd'),'month') from dual; --2019-08-01 00:00:00 sql> select trunc(to_date('20190716','yyyymmdd'),'month') from dual; --2019-07-01 00:00:00 sql> select trunc(sysdate,'year') from dual; --2022-01-01 00:00:00 sql> select to_char(sysdate) from dual; --2022-04-03 07:53:06 sql> select current_date from dual; --2022-04-03 06:59:24 sql> select current_timestamp from dual; --03-apr-22 07.01.50.391024 am 08:00 sql> select dbtimezone from dual; -- 00:00 sql> select sessiontimezone from dual; -- 08:00 sql> select from_tz(timestamp '2000-03-28 08:00:00', '3:00') from dual; --28-mar-00 08.00.00.000000000 am 03:00 sql> select systimestamp from dual; --03-apr-22 07.44.24.146176 am 08:00 sql> select localtimestamp from dual; --03-apr-22 07.15.28.945537 am sql> select new_time(to_date('11-10-09 01:23:45', 'mm-dd-yy hh24:mi:ss'), 'ast', 'pst') "new date and time" from dual; --2009-11-09 21:23:45 sql> select numtodsinterval(100, 'day') from dual; -- 000000100 00:00:00.000000000 sql> select numtoyminterval(1,'year') from dual; -- 000000001-00 sql> select sys_extract_utc(timestamp '2000-03-28 11:30:00.00 -08:00') from dual; --28-mar-00 07.30.00.000000000 pm sql> select to_char(timestamp '2009-01-01 00:00:00' to_dsinterval('p100dt05h'), 'yyyy-mm-dd hh24:mi:ss') "time stamp" from dual; --2009-04-11 05:00:00 sql> select to_timestamp ('10-sep-02 14:10:10.123000', 'dd-mon-rr hh24:mi:ss.ff') from dual; --10-sep-02 02.10.10.123000000 pm sql> select to_timestamp_tz('1999-12-01 11:00:00 -8:00', 'yyyy-mm-dd hh:mi:ss tzh:tzm') from dual; --01-dec-99 11.00.00.000000000 am -08:00 sql> select to_yminterval('01-02')from dual; -- 000000001-02 sql> select tz_offset('us/eastern') from dual; ---04:00
函数 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
sysdate n 加括号 y y 加括号 y
add_months n y y y n y
last_day n y y y 只返回日期 y
months_between n 小数部分不准确 n n n y
next_day n y y y n y
extract (datetime) y y y y y y
round (date) n y n n 结果不一致 y
trunc (date) n y n y n y
to_char (datetime) n y y y n y
- - - - - - -
current_date 只返回日期 只返回日期 只返回日期 只返回日期 只返回日期 只返回日期
current_timestamp y y y y 不带时区 y
dbtimezone n 加括号 n n n 加括号
sessiontimezone n 加括号 n n n 加括号
from_tz n n n n n y
systimestamp n n y n n y
localtimestamp y y y y y y
new_time n n n n n y
numtodsinterval n y y y n y
numtoyminterval n n n n n y
sys_extract_utc n n n n n 结果不准确
to_dsinterval n n n n n y
to_timestamp y y y y n y
to_timestamp_tz n n n n n y
to_yminterval n n n n n y
tz_offset n n n n n n

转换函数

函数 说明
asciistr -
bin_to_num -
cast -
chartorowid -
compose -
convert -
decompose -
hextoraw -
numtodsinterval 同上
numtoyminterval 同上
to_char (character) -
to_char (datetime) 同上
to_char (number) -
to_date -
to_dsinterval 同上
to_multi_byte -
to_number -
to_single_byte -
to_timestamp 同上
to_timestamp_tz 同上
to_yminterval 同上
unistr -
rawtohex -
rawtonhex -
rowidtochar -
rowidtonchar -
scn_to_timestamp -
timestamp_to_scn -
to_binary_double -
to_binary_float -
to_blob -
to_clob -
to_lob -
to_nchar (character) -
to_nchar (datetime) -
to_nchar (number) -
to_nclob -
treat -

上面各函数在 oracle 中执行的例子

sql> select asciistr('abäcde') from dual; --ab\00c4cde sql> select bin_to_num(1,0,1,0) from dual; --10 sql> select cast('22-oct-1997' as timestamp) from dual; --22-oct-97 12.00.00.000000 am sql> select chartorowid('aaafd1aafaaaabsaa/') from dual; --aaafd1aafaaaabsaa/ sql> select compose( 'o' || unistr('\0308') ) from dual; --ö sql> select convert('ä ê í õ ø a b c d e ', 'us7ascii', 'we8iso8859p1') from dual; --a e i ? ? a b c d e ? postgres=# select convert('abc中国'::bytea, 'utf8', 'gbk'); --\x616263d6d0b9fa sql> select hextoraw('4041424344') from dual; --4041424344 sql> select to_char('01110') from dual; --01110 sql> select to_char('01110' 1) from dual; --1111 sql> select to_date('20190715','yyyymmdd') from dual; --2019-07-15 00:00:00 sql> select dump(to_multi_byte( 'a')) from dual; --typ=1 len=2: 163,193 sql> select to_number('1111') from dual; --1111 sql> select to_single_byte('aaa') from dual; --aaa sql> select unistr('abc\00e5\00f1\00f6') from dual; --abcåñö
函数 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
asciistr n n n n n n
bin_to_num n n n n n y
cast y y y y n y
chartorowid n n n n n n
compose n n n n n n
convert y y y y y y
decompose n n n n n n
hextoraw n n y y n n
to_char (character) n n y y n y
to_char (number) n y y y n y
to_date 只返回日期 y y y n y
to_multi_byte n y n n n y
to_number n y y y n y
to_single_byte n y n n n y
unistr n y n n n y
rawtohex - - - - - -
rawtonhex - - - - - -
rowidtochar - - - - - -
rowidtonchar - - - - - -
scn_to_timestamp - - - - - -
timestamp_to_scn - - - - - -
to_binary_double - - - - - -
to_binary_float - - - - - -
to_blob - - - - - -
to_clob - - - - - -
to_lob - - - - - -
to_nchar (character) - - - - - -
to_nchar (datetime) - - - - - -
to_nchar (number) - - - - - -
to_nclob - - - - - -
treat - - - - - -

聚合函数

函数 说明
listagg -
vm_concat -
median -

上面各函数在 oracle 中执行的例子

drop table tt cascade; create table tt (col1 int,col3 char); insert into tt values (1001,'1'); insert into tt values (2001,'2'); insert into tt values (3001,'3'); insert into tt values (4001,'4'); sql> select listagg(col1,':') within group (order by col1) from tt; listagg(col1,':')withingroup(orderbycol1) -------------------------------------------------------------------------------- 1001:1002:2002:3001 sql> select wm_concat(col3) from tt; wm_concat(col3) --------------------------------------------------------------------------------- 1,2,3,4 sql> select median(col1) from tt; median(col1) ------------ 2501
函数 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
listagg n 显式转换 y y n 显式转换
median n y y y n y
vm_concat y y n n n y
  • vm_concat 在 postgresql 和 ivorysql 中只能转换字符类型,不能转换数值类型。
  • vm_concat 在 oracle 用的比较多,opengauss 系列的数据库不支持的话,可以使用下面的方式手工创建这个函数
create or replace function vm_concat_state_func (results text, val text) returns text language sql cost 50 immutable as $$ select results || ',' ||val; $$; create or replace function vm_concat_final_func (results text) returns text language sql cost 111 immutable as $$ select substr(results, 2); $$; create aggregate wm_concat(text) ( sfunc = vm_concat_state_func, stype = text, initcond = '', finalfunc = vm_concat_final_func );

general comparison functions

函数 说明
greatest -
least -

上面各函数在 oracle 中执行的例子

sql> select greatest('harry', 'harriot', 'harold') "greatest" from dual; --harry sql> select greatest(1, 3.925, 2.4) "greatest" from dual; --3.925 sql> select least('harry', 'harriot', 'harold') "greatest" from dual; --harold sql> select least(1, 3.925, 2.4) "greatest" from dual; --1
函数 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
greatest y y y y y y
least y y y y y y

encoding and decoding functions

函数 说明
decode -
dump -
ora_hash -
vsize -

上面各函数在 oracle 中执行的例子

drop table tt; create table tt (col1 int,col3 int); insert into tt values (1001,1),(2001,2),(3001,3),(4001,4); select col1, decode(col3, 1, 'one', 2, 'two', 3, 'three', 'other number') "num-word" from tt; col1 | num-word ------ -------------- 1001 | one 2001 | two 3001 | three 4001 | other number sql> select dump('abc') from dual; --typ=96 len=3: 97,98,99 sql> select vsize('abc') from dual; --3
函数 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
decode n y y y n y
dump n y n n n y
ora_hash - - - - - -
vsize n n n n n y

null-related functions

函数 说明
coalesce -
lnnvl -
nanvl 同上
nullif -
nvl -
nvl2 -

上面各函数在 oracle 中执行的例子

drop table tt; create table tt (col1 int,col3 int); insert into tt values (1002,2000); insert into tt values (2002,null); sql> select * from tt; col1 col3 ---------- ---------- 1002 2000 2002 sql> select col1,col3 from tt where lnnvl( col3 > 2000 ); col1 col3 ---------- ---------- 1002 2000 2002 sql> select col1,nullif(col3,col1) from tt; col1 nullif(col3,col1) ---------- ----------------- 1002 2000 2002 sql> select col1, nvl(col3,0) "nvl" from tt; col1 nvl ---------- ---------- 1002 2000 2002 0 sql> select col1, nvl2(col3,'is not null','is null') from tt; col1 nvl2(col3,' ---------- ----------- 1002 is not null 2002 is null sql> select col1, nvl2(col3,0,1) from tt; col1 nvl2(col3,0,1) ---------- -------------- 1002 0 2002 1
函数 pgsql 13.3 orafce 3.19 og 2.1 og 3.0 mysql 8.0 ivorysql 1.2
coalesce - - - - - -
lnnvl n y n n n y
nullif y y y y y y
nvl n y y y n y
nvl2 n y n n n y

oracle 的其他函数

最后修改时间:2022-04-10 10:05:14
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【米乐app官网下载的版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论

网站地图