《卸甲笔记》-PostgreSQL和Oracle的SQL差异分析之五:函数的差异(一)

PostgreSQL是世界上功能最强大的开源数据库,在国内得到了越来越多机构和开发者的青睐和应用。随着PostgreSQL的应用越来越广泛,Oracle向PostgreSQL数据库的数据迁移需求也越来越多。数据库之间数据迁移的时候,首先是迁移数据,然后就是SQL、存储过程、序列等程序中不同的数据库中数据的使用方式的转换。下面根据自己的理解和测试,写了一些SQL以及数据库对象转换方面的文章,不足之处,尚请多多指教。

1、NULL判断函数

Oracle的NULL判断函数是 nvl(A, B) 和 coalesce 两个函数。nvl(A, B) 是判断如果A不为NULL,则返回A, 否则返回B。参数需要是相同类型的,或者可以自动转换成相同类型的, 否则需要显式转换。而 coalese 参数可以有多个,返回第一个不为NULL的参数。而参数必须为相同类型的 ,不会自动转换。
PostgreSQL中没有nvl函数。但是有coalesce函数。用法和Oracle的一样。可以使用coalesce来转换Oracle的nvl和coalesce。参数需要使用相同类型,或者可以转换成相同类型的。否则需要手动转换。

Oracle NULL判断函数
SQL> select * from o_test;

    VALUE1 VALUE2     VALUE3
---------- ---------- --------------
           111111     05-8月 -16
         1            31-7月 -16
         2 222222

SQL> select nvl(value1, 'Hello') value1 from o_test;
select nvl(value1, 'Hello') value1 from o_test
                   *
第 1 行出现错误:
ORA-01722: 无效数字

SQL> select nvl(value1, '10000') value1 from o_test;

    VALUE1
----------
     10000
         1
         2

SQL> select nvl(value2, 'Hello') value2 from o_test;

VALUE2
----------
111111
Hello
222222

SQL> select nvl(value3, '2010-1-1') value3 from o_test;
select nvl(value3, '2010-1-1') value3 from o_test
                   *
第 1 行出现错误:
ORA-01861: 文字与格式字符串不匹配

SQL> select nvl(value3, to_date( '2010-01-01','YYYY-MM-DD')) value3 from o_test;

VALUE3
--------------
05-8月 -16
31-7月 -16
01-1月 -10

SQL> select coalesce(value1, '10000') value1 from o_test;
select coalesce(value1, '10000') value1 from o_test
                        *
第 1 行出现错误:
ORA-00932: 数据类型不一致: 应为 NUMBER, 但却获得 CHAR

SQL> select coalesce(value1, 10000) value1 from o_test;

    VALUE1
----------
     10000
         1
         2

SQL> select coalesce(value2, '',  'Hello John') value2 from o_test;

VALUE2
----------
111111
Hello John
222222

SQL> select coalesce(value3,'', to_date( '2010-01-01','YYYY-MM-DD')) value3 from o_test;
select coalesce(value3,'', to_date( '2010-01-01','YYYY-MM-DD')) value3 from o_test
                       *
第 1 行出现错误:
ORA-00932: 数据类型不一致: 应为 DATE, 但却获得 CHAR

SQL> select coalesce(value3, null, to_date( '2010-01-01','YYYY-MM-DD')) value3 from o_test;

VALUE3
--------------
05-8月 -16
31-7月 -16
01-1月 -10
PostgreSQL NULL判断函数
postgres=# select * from p_test;
 value1 | value2 |       value3
--------+--------+---------------------
        | 11111  | 2010-01-01 00:00:00
      1 |        | 2010-01-01 00:00:00
      2 | 22222  |
(3 行记录)

postgres=# select coalesce(value1, 'Hello') value1  from p_test;
错误:  无效的整数类型输入语法: "Hello"
第1行select coalesce(value1, 'Hello') value1  from p_test;
                             ^
postgres=# select coalesce(value1, '10000') value1  from p_test;
 value1
--------
  10000
      1
      2
(3 行记录)

postgres=# select coalesce(value2, null, 'Hello world') value2  from p_test;
   value2
-------------
 11111
 Hello world
 22222
(3 行记录)

postgres=# select coalesce(value3, null, '2012-10-10') value3  from p_test;
       value3
---------------------
 2010-01-01 00:00:00
 2016-08-05 10:01:32
 2012-10-10 00:00:00
(3 行记录)

postgres=# select coalesce(value3, null, '2012-10-A') value3  from p_test;
错误:  无效的类型 timestamp 输入语法: "2012-10-A"
第1行select coalesce(value3, null, '2012-10-A') value3  from p_te...
                                   ^

2、字符串连接

2.1、字符串连接符( || )

Oracle的字符串连接符(||) 和 PostgreSQL的字符串连接符(||)的用法基本相同,不同的地方是
1、当连接的参数有null的时候,Oracle中,null 的连接效果类似于空字符串(''),而PostgreSQL中, 连接的参数中有null的, 连接结果统一都是null。
2、当几个参数都是数字的时候,Oracle会自动把数字转换为字符串。这个和Oracle内部的自动类型转换有关系。而PostgreSQL中,几个参数中至少有一个应该为字符串,否则会报错。

数据迁移的时候,对于Oracle的 A || B 可以使用PostgreSQL的coalesce( A, '') || coalesce( B, '')形式来转换。

Oracle 字符串连接符( || )
SQL> select 'abc' || 'def' value from dual;

VALUE
------
abcdef

SQL> select 123 || 456 value from dual;

VALUE
------
123456

SQL> select null || 456 value from dual;

VAL
---
456

SQL> select null || 'abcdef'  value from dual;

VALUE
------
abcdef

SQL> select length(null || null) value from dual;

     VALUE
----------

PostgreSQL 字符串连接符( || )
postgres=# select 'abc' || 'def' as value;
 value
--------
 abcdef
(1 行记录)

postgres=# select 123 || 456 as  value;
错误:  操作符不存在: integer || integer
第1行select 123 || 456 as  value;
                ^
提示:  没有匹配指定名称和参数类型的操作符. 您也许需要增加明确的类型转换.
postgres=# select 123||'456' as value;
 value
--------
 123456
(1 行记录)

postgres=#  select null || 456 as  value ;
 value
-------

(1 行记录)

postgres=# select null || 'abcdef'  as value;
 value
-------

(1 行记录)

postgres=# select length(null || null) as value ;
 value
-------

(1 行记录)

2.2、字符串连接函数concat

Oracle的concat函数类似于字符串连接符(||),但只能够连接两个参数。参数需要是字符串类型,或者可以自动转换成字符串类型。
PostgreSQL中也内置了这个方法。
需要注意的是,Oracle的concat,如果两个参数都是null, 则结果是null。而PostgreSQL中,如果两个参数都是null,则 结果是空字符串('')。因为PostgreSQL的concat方法内部对于参数做了coalesce(null, '')处理。

Oracle concat
SQL> select concat('abc','def') from dual;

CONCAT
------
abcdef

SQL> select concat(123, 456) from dual;

CONCAT
------
123456

SQL> select concat(null, 456) value from dual;

VAL
---
456

SQL> select concat(null, 'abc') value from dual;

VAL
---
abc

SQL> select concat(null, null) value from dual;

V
-


SQL> select length(concat(null, null)) value from dual;

     VALUE
----------

SQL> select * from o_test;

    VALUE1 VALUE2     VALUE3
---------- ---------- --------------
           111111     05-8月 -16
         1            31-7月 -16
         2 222222

SQL> select concat(value3, value2) from o_test;

CONCAT(VALUE3,VALUE2)
------------------------
05-8月 -16111111
31-7月 -16
222222

PostgreSQL 字符串连接函数
postgres=# select concat('abc','def');
 concat
--------
 abcdef
(1 行记录)

postgres=# select concat(123, 456);
 concat
--------
 123456
(1 行记录)

postgres=# select concat(null, 456) as value;
 value
-------
 456
(1 行记录)


postgres=# select concat(null, 'abc') as value;
 value
-------
 abc
(1 行记录)

postgres=# select concat(null, null) as value;
 value
-------

(1 行记录)

postgres=# select length(concat(null, null)) as value;
 value
-------
     0
(1 行记录)

postgres=# select * from p_test;
 value1 | value2 |       value3
--------+--------+---------------------
        | 11111  | 2010-01-01 00:00:00
      1 |        | 2016-08-05 10:01:32
      2 | 22222  |
(3 行记录)

postgres=# select concat(value3, value2)   as value from p_test;
          value
--------------------------
 2010-01-01 00:00:0011111
 2016-08-05 10:01:32
 22222
(3 行记录)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值