Varchar to Decimal conversion in db2

ぃ、小莉子 提交于 2019-12-14 02:43:56

问题


I am trying to convert a varchar field to decimal field in db2 but I am getting an error

AMOUNT field is a varchar field which needs to be converted to decimal(31,3)

Given below is my query

SELECT CAST(ROUND(TRIM(AMOUNT),3) AS DECIMAL(31,3))
FROM TABLENAME

Note: AMOUNT field (varchar) has a NULL value as well
Sample values:

7.324088  
-42.97209  
854  
NULL  
6  
6  
350  
-6  
15.380584  
1900

I get the below error:

Invalid character found in a character string argument of the function "DECFLOAT". SQLSTATE=22018


回答1:


Try doing like

SELECT CAST(ROUND(COALESCE(TRIM(AMOUNT),0),3) AS DECIMAL(31,3))
FROM TABLENAME



回答2:


Are you sure your input values correspond with the same locale. I mean is the minus sign a minus or the MS Word minus? What about the decimal separator? In some languages there is a comma instead of a dot.

The coalesce function retrieves the first non-null value from the parameters. In the example:

coalesce (amount, 0)

If amount is null, it will return 0.

Are you sure you are passing a null value, or a string with the characters 'NULL', because in this case you have to convert the 'NULL' to '0' with the replace function.

replace (amount, 'NULL', '0')


来源:https://stackoverflow.com/questions/23227240/varchar-to-decimal-conversion-in-db2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!