文档中心MogDBMogDB StackUqbar
v5.0

文档:v5.0

支持的版本:

其他版本:

类型转换函数

类型转换函数

  • cast(x as y)

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

    示例:

    MogDB=# set dolphin.b_compatibility_mode=on;
    
    MogDB=# SELECT cast('abc' as char(10));
     varchar
    ---------
     abc
    (1 row)
    
    MogDB=# SELECT dolphin.b_compatibility_mode=off;
    
    MogDB=# SELECT cast('abc' as char(10));
       bpchar
    ------------
     abc
    (1 row)

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

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

兼容性

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

MogDB=# SELECT CAST('$2'::money as unsigned);
  uint8
  -------
  2
  (1 row)
MogDB=# SELECT CAST(CURRENT_TIMESTAMP::TIMESTAMP AS UNSIGNED);
  current_timestamp
  -------------------
  20230103023621
  (1 row)
Copyright © 2011-2024 www.enmotech.com All rights reserved.