VastbaseG100

基于openGauss内核开发的企业级关系型数据库。

Menu

+运算符精度处理

功能描述

Vastbase G100在MySQL兼容模式下使用“+”运算符时,若左值为“0”且右值为任意带数字的字符串(charnum),则0 + charnum将得到与原字符串charnum中相同精度的浮点数或者整数。

语法格式

0 + charnum

参数说明

charnum

带数字的字符串。

注意事项

  • 该功能仅在数据库兼容模式为MySQL时支持(即数据库实例初始化时指定DBCOMPATIBILITY='B')。

  • 仅Vastbase V2.2 Build 10(Patch No.7)及以后版本支持此功能。

  • 当执行cast(x as char)时,由于char限制长度与MySQL表现不一致,需设置GUC参数b_compatibility_mode为on,cast(x as char)等价于cast(x as varchar),执行结果与MySQL表现一致。

示例

示例1:通过cast(x as y)函数进行类型转换。

cast(x as y)为类型转换函数,用于将x转换成y指定的类型。参见类型转换函数

1、select查看cast(x as y)的结果:

select cast(2/3 as char(6));

返回结果如下:

 bpchar
--------
 .66666
(1 row)

2、查看0 + cast(x as y)的结果。

select 0+cast(2/3 as char(6));

返回结果如下,0 + cast(x as y)的结果精度与cast(x as y)的返回值精度相同。

 ?column?
----------
   .66666
(1 row)

示例2:在DML语句中使用0+cast(x as y)特性。

1、创建测试表插入数据。

create table student(stdno int,student_age char(20),grade float8);
insert into student values(001,'15.00',81.00);
insert into student values(002,'16.00',82.01);
insert into student values(003,'17.00',81.02);
insert into student values(004,'13.00',83.50);
insert into student values(005,'14.00',84.00);
insert into student values(006,'15.00',89.50);
insert into student values(007,'19.000',91.00);
insert into student values(008,'12.0000',93);
insert into student values(008,'11',61.0);

2、查看表student的数据,根据列stdno进行升序排序。

select * from student order by stdno;

返回结果如下:

 stdno |     student_age      | grade
-------+----------------------+-------
     1 | 15.00                |    81
     2 | 16.00                | 82.01
     3 | 17.00                | 81.02
     4 | 13.00                |  83.5
     5 | 14.00                |    84
     6 | 15.00                |  89.5
     7 | 19.000               |    91
     8 | 12.0000              |    93
     8 | 11                   |    61
(9 rows)

3、执行update操作。

UPDATE student SET student_age= (0 + CAST(student_age AS VARCHAR)) WHERE 1=1;

4、更新成功,查看更新的结果。

select * from student order by stdno;

返回结果如下:

 stdno |                    student_age                     | grade
-------+----------------------------------------------------+-------
     1 | 15                                                 |    81
     2 | 16                                                 | 82.01
     3 | 17                                                 | 81.02
     4 | 13                                                 |  83.5
     5 | 14                                                 |    84
     6 | 15                                                 |  89.5
     7 | 19                                                 |    91
     8 | 12                                                 |    93
     8 | 11                                                 |    61
(9 rows)

5、恢复原始环境。

drop table student;