SQL,将数据类型varchar转换为数字时出错

时间:2013-05-30 21:01:36

标签: sql

SELECT LIFNR,
 SUM(CASE WHEN  UPPER([WAERS])='USD' THEN ROUND(CONVERT(numeric(10,2),DMBTR)* (-1),2)
               WHEN UPPER(WAERS)='USD' THEN ROUND(CONVERT(numeric(10,2),DMBTR),2) 
               ELSE ROUND(CONVERT(numeric(10,2),DMBTR),2)
               END)
AS 'AMOUNT'
FROM tbl_BSAK
GROUP BY LIFNR;

大家好。

我刚写了一段代码,我不断收到错误消息“将数据类型varchar转换为数字时出错”。我确定这是一个简单的修复,但我花了太多时间进行故障排除...

请帮帮我!!!!谢谢你!

1 个答案:

答案 0 :(得分:4)

根据语法,我假设这是SQL Server。

如果要查找有问题的值,请执行以下操作:

select *
FROM tbl_BSAK
where isnumeric(DMBTR) = 0 and DMBTR is not null;

要解决此问题,请执行以下操作:

SELECT LIFNR,
 SUM(CASE WHEN isnumeric(DMBTR) = 1and UPPER([WAERS])='USD' THEN ROUND(CONVERT(numeric(10,2),DMBTR)* (-1),2)
               WHEN isnumeric(DMBTR) = 1 and UPPER(WAERS)='USD' THEN ROUND(CONVERT(numeric(10,2),DMBTR),2) 
               when isnumeric(DMBTR) = 1 then ROUND(CONVERT(numeric(10,2),DMBTR),2)
               END)
AS 'AMOUNT'
FROM tbl_BSAK
GROUP BY LIFNR;