当前位置:   article > 正文

PostgreSQL Oracle兼容性之 - text - text隐式转换

postgres 的::text 对应 oracle

标签

PostgreSQL , Oracle , 兼容性 , text减text操作符


背景

Oracle的两个文本详见,会自动转换为数值进行相减操作。

PostgreSQL默认并不会将文本转换为数值进行相减操作。

为了实现兼容,有两种方法:

1、创建text到numeric的隐式转换

2、创建text-text的操作符。

PostgreSQL 内置CAST

可以看到varchar是有隐式转numeric的。但是text类型没有隐式转numeric。

  1. postgres=# \dC
  2. List of casts
  3. Source type | Target type | Function | Implicit?
  4. -----------------------------+-----------------------------+---------------------------+---------------
  5. ...........
  6. character varying | "char" | char | in assignment
  7. character varying | character | (binary coercible) | yes
  8. character varying | character varying | varchar | yes
  9. character varying | name | name | yes
  10. character varying | numeric | (binary coercible) | yes
  11. character varying | regclass | regclass | yes
  12. character varying | text | (binary coercible) | yes
  13. character varying | xml | xml | no
  14. ..........
  15. text | "char" | char | in assignment
  16. text | character | (binary coercible) | yes
  17. text | character varying | (binary coercible) | yes
  18. text | name | name | yes
  19. text | regclass | regclass | yes
  20. text | xml | xml | no
  21. .................
  22. (241 rows)

因此如果你使用的是varchar-varchar,可以自动算出结果来。

  1. postgres=# select '1'::varchar - '2.1'::varchar;
  2. ?column?
  3. ----------
  4. -1.1
  5. (1 row)

但是使用text-text就得不到结果。

  1. postgres=# select '1'::text - '2'::text;
  2. ERROR: operator does not exist: text - text
  3. LINE 1: select '1'::text - '2'::text;
  4. ^
  5. HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

1、隐式转换方法

1、创建隐式转换的语法

  1. postgres=# \h create cast
  2. Command: CREATE CAST
  3. Description: define a new cast
  4. Syntax:
  5. CREATE CAST (source_type AS target_type)
  6. WITH FUNCTION function_name [ (argument_type [, ...]) ]
  7. [ AS ASSIGNMENT | AS IMPLICIT ]
  8. CREATE CAST (source_type AS target_type)
  9. WITHOUT FUNCTION
  10. [ AS ASSIGNMENT | AS IMPLICIT ]
  11. CREATE CAST (source_type AS target_type)
  12. WITH INOUT
  13. [ AS ASSIGNMENT | AS IMPLICIT ]

2、创建text隐式转numeric

  1. postgres=# create cast (text as numeric) with inout AS IMPLICIT ;
  2. CREATE CAST

3、现在可以做减法了

  1. postgres=# select '1'::text - '2'::text;
  2. ?column?
  3. ----------
  4. -1
  5. (1 row)
  6. postgres=# select '1'::text - '2.1'::text;
  7. ?column?
  8. ----------
  9. -1.1
  10. (1 row)

2、创建text-text操作符方法

第二种方法是使用新建操作符的方法.

1、创建运算函数

  1. create or replace function text_text(text,text) returns numeric as $$
  2. select $1::numeric-$2::numeric;
  3. $$ language sql strict immutable;
  4. CREATE FUNCTION

测试运算函数

  1. postgres=# select text_text('1.1', '2.222');
  2. text_text
  3. -----------
  4. -1.122
  5. (1 row)

2、基于运算函数,创建操作符

  1. postgres=# create operator - (procedure=text_text, leftarg=text, rightarg=text);
  2. CREATE OPERATOR

3、现在可以支持text-text了。

为了验证操作符的效果,先把前面创建的隐式转换删掉

  1. postgres=# drop cast (text as numeric);
  2. DROP CAST
  1. postgres=# select '1.1'::text-'1.2'::text;
  2. ?column?
  3. ----------
  4. -0.1
  5. (1 row)
  6. postgres=# select '1.1'::text-'1.22'::text;
  7. ?column?
  8. ----------
  9. -0.12
  10. (1 row)

参考

《PostgreSQL 整型int与布尔boolean的自动转换设置(含自定义cast与cast规则介绍)》

《PostgreSQL 自定义自动类型转换(CAST)》

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/870392
推荐阅读
相关标签
  

闽ICP备14008679号