简体   繁体   English

db2:十进制列到varchar列

[英]Db2 : decimal column to varchar column

My db2 version is 9.5: 我的db2版本是9.5:

I have a select query as below: 我有一个选择查询,如下所示:

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

This results as below: 结果如下:

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

I want to use this query as below: 我想按以下方式使用此查询:

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

However, the SUM column in mySchema.myTable1 is of type VARCHAR(40) (which I can not change to decimal because of other reasons). 但是,mySchema.myTable1中的SUM列的类型为VARCHAR(40)(由于其他原因,我无法将其更改为十进制)。 Because of which I am getting below error: 因此,我得到以下错误:

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

When I try the following: 当我尝试以下操作时:

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

the values becomes: 值变为:

00000000000000000000000000000.00 
00000000000000000000000000000.00 
00000000000000000000002475000.00 
00000000000000000000000550000.00 
00000000000000000000000746966.09 

which I do not want. 我不想要的。

How can I format such that the values comes as String as is: 我如何格式化以使这些值按原样作为String出现:

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

Thanks for reading! 谢谢阅读!

Try to use VARCHAR_FORMAT() function 尝试使用VARCHAR_FORMAT()函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM