簡體   English   中英

將臨時表列(十進制)轉換為十進制,但仍會收到“將varchar轉換為數值數據類型的算術溢出錯誤”

[英]Converting temptable column (Decimal) to Decimal but still getting “Arithmetic overflow error converting varchar to data type numeric”

我在臨時表中進行選擇時遇到問題,但我將其轉換為浮點數以確保值正確。 但是當我運行查詢時,出現以下錯誤

將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 
     ( MoveF.AccountNumber=@AccountNumber) and        
        MCodeF.MovementCode=MoveF.MovementCode and
      MoveF.ValueDate>=@FromDate and
      MoveF.ValueDate<=@ToDate
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 
      MoveF.AccountNumber=@AccountNumber and
      MoveF.ValueDate<@FromDate and
      MoveF.DebitCredit='W'),0)
-      
isnull((Select SUM(MoveF.MoveAmount)
From CashMovements as MoveF
Where (MoveF.AccountNumber=@AccountNumber) 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

我得到的錯誤:

Msg 8115,第16級,狀態8,第67行
將varchar轉換為數值數據類型的算術溢出錯誤。

魔術發生在最后一個選擇中,但它在第二個case語句中出現錯誤,我知道我可以使用IIF ,但是我只有SQL Server 2008

您可能會嘗試在varchar值的replace() 之后轉換為decimalfloat

 , 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)

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

declare @f float = '123456789012345.123';

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

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

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM