VastbaseG100

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

Menu

STR

功能描述

Vastbase G100在SQL Server兼容模式下支持STR函数,用于返回数字数据转换来的字符数据,具有指定长度和十进制精度。返回值为varchar类型,转换所得的字符串。

语法格式

STR(float_expression[,length [,decimal]])

参数说明

  • float_expression

    带小数点的近似数字,float数据类型表达式。

  • length

    总长度,包括小数点、符号、数字以及空格。

    默认值:10

  • decimal

    小数点后的位数。

    默认值:0

注意事项

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

示例

示例1:直接调用STR函数。

select str(123.45,6,1);

结果返回如下:

   str
---------
   123.5
(1 row)

示例2:对行存表使用DML语句调用STR函数。

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

CREATE table test_tb_1132253(id int, income varchar);
insert into test_tb_1132253 values(1, '12.3462754');
insert into test_tb_1132253 values(2, '222.3462754');
insert into test_tb_1132253 values(3, '333.3462754');

2、SELECT语句中调用STR函数处理返回结果。

select id, STR(income, 6, 2) from test_tb_1132253;

结果返回如下:

 id |   str
----+---------
  1 |   12.35
  2 |  222.35
  3 |  333.35
(3 rows)

3、UPDATE语句中调用STR函数处理数据进行更新。

update test_tb_1132253 set income = STR(777.889741, 10, 4) where id = 2;
select * from test_tb_1132253 order by id;

结果返回如下:

 id |   income
----+-------------
  1 | 12.3462754
  2 |    777.8897
  3 | 333.3462754
(3 rows)

4、DELETE语句中调用STR函数删除数据。

delete from test_tb_1132253 where income = STR(12.346275427, 10, 7);
select * from test_tb_1132253 order by id;

结果返回如下:

 id |   income
----+-------------
  2 |    777.8897
  3 | 333.3462754
(2 rows)