Oracle和MySQL的常用函数比较(一)

image.png

按照db-engines的最新排名,Oracle和MySQL依旧占据榜单的前列,作为业界影响力的老大和老二,两者在具体应用中也存在不少差异,我们今天就其支持的函数做一个对比。
数据库函数给用户提供了一种便利的扩展手段,只要指定参数,函数就会返回期待的结果。中间实现细节用户不用关心。
我们将常用函数分为以下几类:字符串函数、数值函数、日期函数、转换函数
我们一一做些比较和学习,我们先就常用的字符串函数进行比较和学习。

字符串函数

  • 大小写转换(upper,lower):
  • Oracle

SQL> select upper('hello world') from dual;

UPPER('HELL
-----------
HELLO WORLD

SQL> select lower('HELLO WORLD') from dual;

LOWER('HELL
-----------
hello world

SQL> 
  • MySQL

mysql> select upper('hello world') ;
+----------------------+
| upper('hello world') |
+----------------------+
| HELLO WORLD          |
+----------------------+
1 row in set (0.00 sec)

mysql> select lower('HELLO WORLD') ;
+----------------------+
| lower('HELLO WORLD') |
+----------------------+
| hello world          |
+----------------------+
1 row in set (0.00 sec)

mysql> 
  • 字符串连接(concat):
  • Oracle
SQL> select concat('hello','world') from dual;

CONCAT('HE
----------
helloworld
  • MySQL
mysql> select concat('hello','world');
+-------------------------+
| concat('hello','world') |
+-------------------------+
| helloworld              |
+-------------------------+
1 row in set (0.00 sec)

mysql> 
  • 字符串截取(substr,left,right):
  • Oracle
SQL> select substr('hello world',1,5) from dual;

SUBST
-----
hello
  • MySQL
mysql> select substring('hello world',1,5);
+------------------------------+
| substring('hello world',1,5) |
+------------------------------+
| hello                        |
+------------------------------+
1 row in set (0.00 sec)
mysql> select substr('hello world',1,5);
+---------------------------+
| substr('hello world',1,5) |
+---------------------------+
| hello                     |
+---------------------------+
1 row in set (0.00 sec)
mysql> select left('hello world',5);
+-----------------------+
| left('hello world',5) |
+-----------------------+
| hello                 |
+-----------------------+
1 row in set (0.00 sec)

mysql> select right('hello world',5);
+------------------------+
| right('hello world',5) |
+------------------------+
| world                  |
+------------------------+
1 row in set (0.00 sec)

mysql> 
  • 字符串复制(repeat):
  • Oracle
    没有字符串复制函数
  • MySQL
mysql> select repeat('hello',2);
+-------------------+
| repeat('hello',2) |
+-------------------+
| hellohello        |
+-------------------+
1 row in set (0.00 sec)
  • 字符串替换(replace):
  • Oracle
SQL> select replace('hello world','hello','kitty') from dual;

REPLACE('HE
-----------
kitty world

SQL> select translate('12345','135','abc') from dual;

TRANS
-----
a2b4c

SQL> 
  • MySQL
mysql> select replace('hello world','hello','kitty');
+----------------------------------------+
| replace('hello world','hello','kitty') |
+----------------------------------------+
| kitty world                            |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> 
  • 字符串长度计算(length):
  • Oracle
SQL> select length('hello world') from dual;

LENGTH('HELLOWORLD')
--------------------
                  11

SQL> 
  • MySQL
mysql> select length('hello world') ;
+-----------------------+
| length('hello world') |
+-----------------------+
|                    11 |
+-----------------------+
1 row in set (0.00 sec)

mysql> 
  • 字符串查找(instr):
  • Oracle
SQL> select instr('hello world','hello') from dual;

INSTR('HELLOWORLD','HELLO')
---------------------------
                          1

SQL> 
  • MySQL
mysql> select instr('hello world','hello') ;
+------------------------------+
| instr('hello world','hello') |
+------------------------------+
|                            1 |
+------------------------------+
1 row in set (0.00 sec)

mysql>
  • 字符转ASCII码(ascii):
  • Oracle
SQL> select ascii('a') from dual;

ASCII('A')
----------
        97
  • MySQL
mysql> select ascii('a') ;
+------------+
| ascii('a') |
+------------+
|         97 |
+------------+
1 row in set (0.00 sec)

mysql> 
  • ASCII码转字符(chr,char):
  • Oracle
SQL> select chr(97) from dual;

C
-
a

SQL>
  • MySQL
mysql> select char(97);
+----------+
| char(97) |
+----------+
| a        |
+----------+
1 row in set (0.00 sec)
  • 去空格(trim,ltrim,rtrim):
  • Oracle
SQL> select trim(' hello ') from dual;

TRIM(
-----
hello

SQL> select ltrim(' hello ') from dual;

LTRIM(
------
hello


SQL> select rtrim(' hello ') from dual;

RTRIM(
------
 hello

SQL> 
  • MySQL
mysql> select trim(' hello ');
+-----------------+
| trim(' hello ') |
+-----------------+
| hello           |
+-----------------+
1 row in set (0.00 sec)

mysql> select ltrim(' hello ');
+------------------+
| ltrim(' hello ') |
+------------------+
| hello            |
+------------------+
1 row in set (0.00 sec)

mysql> select rtrim(' hello ');
+------------------+
| rtrim(' hello ') |
+------------------+
|  hello           |
+------------------+
1 row in set (0.00 sec)

mysql> 
  • 字符串填充(lpad,rpad):
  • Oracle
SQL> select lpad('hello',10,'*') from dual;

LPAD('HELL
----------
*****hello

SQL> select rpad('hello',10,'*') from dual;

RPAD('HELL
----------
hello*****

SQL> 
  • MySQL
mysql> select lpad('hello',10,'*');
+----------------------+
| lpad('hello',10,'*') |
+----------------------+
| *****hello           |
+----------------------+
1 row in set (0.00 sec)

mysql> select rpad('hello',10,'*');
+----------------------+
| rpad('hello',10,'*') |
+----------------------+
| hello*****           |
+----------------------+
1 row in set (0.00 sec)

mysql> 
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 常见区别 1. 连接字符串在Oracle中用“||”或者concatt('a','b'),SqlServer中用+...
    captain_fu阅读 1,641评论 0 10
  • 1.简介 数据存储有哪些方式?电子表格,纸质文件,数据库。 那么究竟什么是关系型数据库? 目前对数据库的分类主要是...
    乔震阅读 2,047评论 0 2
  • 1、字符串函数 ascii(str) 返回字符串str的第一个字符的ascii值(str是空串时返回0) mysq...
    安易学车阅读 321评论 0 2
  • 转自公众号‘java一日一条’ 1.字符串函数 ascii(str) 返回字符串str的第一个字符的ascii值(...
    LOOK_LOOK阅读 1,132评论 0 1
  • 欢乐永远是难么短暂,回过头,依然要奋起直追!人生就是一段没有尽头的旅行,期待晨曦,向往星空!原来,一路阳光,颠簸路...
    格物者阅读 176评论 2 0

友情链接更多精彩内容