类型转换函数

类型转换函数

  • cast(x as y)

    描述:类型转换函数,将x转换成y指定的类型。如果目标类型为char类型,在dolphin.b_compatibility_mode=on的情况下,实际转换为varchar类型;否则仍为char类型。

    示例:

    1. openGauss=# set dolphin.b_compatibility_mode=on;
    2. openGauss=# SELECT cast('abc' as char(10));
    3. varchar
    4. ---------
    5. abc
    6. (1 row)
    7. openGauss=# SELECT dolphin.b_compatibility_mode=off;
    8. openGauss=# SELECT cast('abc' as char(10));
    9. bpchar
    10. ------------
    11. abc
    12. (1 row)

在后续开发中,扩展的CAST转换功能有money到unsigned和timestamp到unsigned的转换。

  1. CREATE CAST (timestamp AS uint8) WITH FUNCTION timestamp_uint8(timestamp) AS ASSIGNMENT;
  2. CREATE CAST (money AS uint8) WITH FUNCTION cash_uint(money) AS ASSIGNMENT;

兼容性

CREATE CAST指令符合SQL标准,除了SQL没有为二进制可强制转换类型或者实现函数的额外参数来实现功能。

  1. openGauss=# SELECT CAST('$2'::money as unsigned);
  2. uint8
  3. -------
  4. 2
  5. (1 row)
  6. openGauss=# SELECT CAST(CURRENT_TIMESTAMP::TIMESTAMP AS UNSIGNED);
  7. current_timestamp
  8. -------------------
  9. 20230103023621
  10. (1 row)

```