2017-03-29 29 views
0

我在那里,我从一个临时表中选择一个问题,但我做一个转换为浮动,以保证价值正确。但是当我运行查询,我得到以下错误转换不是Temptable柱(十进制)以十进制,但仍然得到“算术溢出错误转换VARCHAR数据类型数字”

算术溢出错误转换VARCHAR数据类型数字

即使我不选择这里varchar列声明:

declare @AccountNumber varchar(25) 
declare @FromDate datetime 
declare @ToDate datetime 

set @AccountNumber='ROSECALL' 
set @FromDate='1980/01/01' 
set @ToDate='2017/01/01' 

Create table #TransF(RowId int NOT NULL identity(1,1), ValueDate datetime, MovementType varchar(50), Remarks varchar(100), Dr Decimal(19,2), 
    Cr Decimal(19,2), Balance float) 

insert into #TransF (ValueDate, MovementType, Remarks, Dr, Cr) 
values (@FromDate, 'Opening Balance', '',0,0); 

insert into #TransF (ValueDate, MovementType, Remarks, Dr, Cr) 
Select MoveF.ValueDate, MCodeF.Description, 

MoveF.Comment, 
(select (
      case (MoveF.Action) 
      when 'W' then MoveF.MoveAmount 
       else 0 
      end 
     )), 
(select (
      case (MoveF.Action) 
      when 'D' then -MoveF.MoveAmount 
       else 0 
      end 
     )) 
From CashMovements as MoveF, 
    MovementCodes as MCodeF 
Where 
    ([email protected]) and   
     MCodeF.MovementCode=MoveF.MovementCode and 
     MoveF.ValueDate>[email protected] and 
     MoveF.ValueDate<[email protected] 
Order By MoveF.ValueDate 

insert into #TransF (ValueDate, MovementType, Remarks, Dr, Cr) 
values (@ToDate, 'Closing Balance','',0,0); 


Update #TransF set Balance= 
(select (isnull((Select SUM(MoveF.MoveAmount) 
From CashMovements as MoveF 
Where 
     [email protected] and 
     MoveF.ValueDate<@FromDate and 
     MoveF.DebitCredit='W'),0) 
-  
isnull((Select SUM(MoveF.MoveAmount) 
From CashMovements as MoveF 
Where ([email protected]) and 
     MoveF.ValueDate<@FromDate and 
     MoveF.DebitCredit='DC'),0))) 
where RowId=1 

SELECT 2 AS RowId INTO #RowID 
while ((SELECT TOP 1 RowId FROM #RowID)<=(select count(1) from #TransF)) 
begin 
    Update #TransF set Balance=((select Balance from #TransF where RowId=(SELECT TOP 1 RowId FROM #RowID)-1)+Dr-Cr) where RowId=(SELECT TOP 1 RowId FROM #RowID) 
    UPDATE #RowID SET RowId = (SELECT TOP 1 RowId FROM #RowID) +1; 
end; 


Select TransF.ValueDate as 'Value Date', 
     TransF.MovementType as 'Movement Type', 
     TransF.Remarks as 'Remarks', 
     Case when ((replace(CONVERT(Decimal(19,2) , TransF.DR, 1),',',' ') = 0.00))then 0 else (replace(CONVERT(Decimal(19,2), TransF.Dr, 1),',',' ')) end as 'Debit', 
     Case when ((replace(CONVERT(Decimal(19,2), TransF.CR, 1),',',' ') = 0.00))then 0 else (replace(CONVERT(Decimal(19,2),TransF.CR, 1),',',' ')) end as 'Credit', 

      Cast(TransF.Balance as decimal)as 'Balance' 
      From #TransF as TransF 
       Order By TransF.RowId 
       Drop table #TransF 
       DROP TABLE #RowID 

我得到的错误:

消息8115 ,级别16,状态8,行67
将varchar转换为数据类型数值的算术溢出错误。

魔术发生在最后一个选择,但其在第二case语句窃听,我知道我可以使用IIF,但我只有SQL Server 2008的

回答

0

你是可以尝试转换成decimalfloatreplace()上的varchar值。

, case 
    when CONVERT(decimal(16, 2),replace(TransF.DR, ',', ' ')) = 0.00 
    then 0 
    else CONVERT(decimal(16, 2),replace(TransF.DR, ',', ' ')) 
    end as 'Debit' 
, case 
    when CONVERT(float,replace(TransF.CR, ',', ' '))= 0.00 
    then 0 
    else CONVERT(float,replace(TransF.CR, ',', ' ')) 
    end as 'Credit' 
, 

您可能需要使用较大的decimal。尝试decimal(19,2)decimal(28,2),或decimal(38,2)

rextester演示:http://rextester.com/HUNUF16216

declare @f float = '123456789.123'; 

select convert(decimal(19,2),@f); -- works 

select convert(decimal(16,2),@f); -- overflow error 

参考:

+0

我也试过,但在第二case语句出现错误 –

+0

@ Reeco.rr更新 – SqlZim

相关问题