Db2:十进制列到varchar列

时间:2014-04-11 09:52:18

标签: sql database db2

我的db2版本是9.5:

我有一个选择查询,如下所示:

select sum(BAL_BOOK_AMT) as sum from mySchema.myTable group by idCol

结果如下:

  SUM
---------
 0
 2,475,000
 746,966.091
-627,247.63

我想使用此查询,如下所示:

insert into mySchema.myTable1 select sum(bal_book_amt) as sum from mySchema.myTable group by idCol

但是,mySchema.myTable1中的SUM列的类型为VARCHAR(40)(由于其他原因,我无法将其更改为十进制)。因为我得到了以下错误:

 by: com.ibm.db2.jcc.b.nm: DB2 SQL Error: SQLCODE=-408, SQLSTATE=42821

当我尝试以下操作时:

select char(sum(BAL_BOOK_AMT)) as sum from mySchema.myTable group by idCol

值变为:

00000000000000000000000000000.00 
00000000000000000000000000000.00 
00000000000000000000002475000.00 
00000000000000000000000550000.00 
00000000000000000000000746966.09 

我不想要。

如何进行格式化以使值按字符串形式出现:

 0
 2,475,000
 746,966.091
-627,247.63

感谢阅读!

1 个答案:

答案 0 :(得分:0)

尝试使用VARCHAR_FORMAT() function

insert into mySchema.myTable1 
select VARCHAR_FORMAT(sum(bal_book_amt), '999,999,999,999.99') as sum
....