博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《卸甲笔记》-PostgreSQL和Oracle的SQL差异分析之五:函数的差异(一)
阅读量:7225 次
发布时间:2019-06-29

本文共 5980 字,大约阅读时间需要 19 分钟。

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 222222SQL> 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         2SQL> select nvl(value2, 'Hello') value2 from o_test;VALUE2----------111111Hello222222SQL> 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月 -1631-7月 -1601-1月 -10SQL> select coalesce(value1, '10000') value1 from o_test;select coalesce(value1, '10000') value1 from o_test                        *第 1 行出现错误:ORA-00932: 数据类型不一致: 应为 NUMBER, 但却获得 CHARSQL> select coalesce(value1, 10000) value1 from o_test;    VALUE1----------     10000         1         2SQL> select coalesce(value2, '',  'Hello John') value2 from o_test;VALUE2----------111111Hello John222222SQL> 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, 但却获得 CHARSQL> select coalesce(value3, null, to_date( '2010-01-01','YYYY-MM-DD')) value3 from o_test;VALUE3--------------05-8月 -1631-7月 -1601-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------abcdefSQL> select 123 || 456 value from dual;VALUE------123456SQL> select null || 456 value from dual;VAL---456SQL> select null || 'abcdef'  value from dual;VALUE------abcdefSQL> 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------abcdefSQL> select concat(123, 456) from dual;CONCAT------123456SQL> select concat(null, 456) value from dual;VAL---456SQL> select concat(null, 'abc') value from dual;VAL---abcSQL> 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 222222SQL> select concat(value3, value2) from o_test;CONCAT(VALUE3,VALUE2)------------------------05-8月 -1611111131-7月 -16222222
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 行记录)

转载地址:http://jieym.baihongyu.com/

你可能感兴趣的文章
【数据结构作业心得】4-1 指针笔记
查看>>
oracle拼接字段用||
查看>>
rabbitmq /usr/lib/rabbitmq/bin/rabbitmq-server: line 85: erl: command not found
查看>>
编程语言学习清单
查看>>
IO流的复习笔记
查看>>
MySQL Connector Net连接vs2012问题
查看>>
LeetCode – Refresh – Merge Intervals
查看>>
UDP编程简单案例
查看>>
Exce信息提取
查看>>
基于c的简易计算器一
查看>>
POJ1995 ZOJ2150 Raising Modulo Numbers【快速模幂】
查看>>
NTT学习笔记
查看>>
online_judge_1051
查看>>
Mac与Widow下编译与运行java文件引入多个外部jar包
查看>>
通过Bottledwater同步PostgreSQL中的数据变化到Kafka消息队列
查看>>
CSS Hack兼容
查看>>
ASP.NET WebService 中使用 ASP.NET_SessionId
查看>>
洛谷P4112 最短不公共子串
查看>>
初识 go 语言:语法
查看>>
Every Tom,Dick and Harry. 不管张三李四。
查看>>