简体   繁体   中英

Arithmetic Overflow Error in SQL Server 2005 query

I am trying to write a simple query to show me differences in dates between sales order delivery date and purchase order delivery date where there is a link between the two.

I am getting an error when I execute the query below:

SELECT T0.[DocNum], T0.[CardCode], T0.[CardName], T0.[DocDueDate], T1.[LineNum], T1.[ItemCode], T1.[Dscription], T1.[Quantity], T1.[POTRGNUM],T1.[ShipDate] as 'SO Delivery Date', T1.[U_ShipDetl] as 'PO Delivery Date' 

FROM ORDR T0  INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry 

WHERE T0.[DocStatus] ='O' AND T1.[POTRGNUM] IS NOT NULL and (T1.[ShipDate] > T1.[U_ShipDetl])

The error message reads:

Arithmetic overflow error converting expression to data type datetime.

I notice that one of the fields I am trying to use (t1.u_shipdetl) has been user created so is it possible that is hasn't been created properly?

Thanks

One of ShipDate and U_ShipDetl are not datetime data types, and the attempt to convert that one to a datetime value is failing. The Arithmetic overflow error suggests it is trying to convert from a numeric type. Perhaps the value is stored as 20170509 ? If so, this may work:

select
    T0.[DocNum]
  , T0.[CardCode]
  , T0.[CardName]
  , T0.[DocDueDate]
  , T1.[LineNum]
  , T1.[ItemCode]
  , T1.[Dscription]
  , T1.[Quantity]
  , T1.[POTRGNUM]
  , T1.[ShipDate]   as 'SO Delivery Date'
  , T1.[U_ShipDetl] as 'PO Delivery Date'
from ORDR T0
  inner join RDR1 T1 
    on T0.DocEntry = T1.DocEntry
where T0.[DocStatus] = 'O' 
  and T1.[POTRGNUM] is not null 
  and T1.[ShipDate] > convert(date,convert(char(8),T1.[U_ShipDetl]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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