SQL Server 2005查询中的算术溢出错误

时间:2017-05-10 16:34:47

标签: sql-server

我正在尝试编写一个简单的查询,以显示销售订单交货日期和采购订单交货日期之间的日期差异,其中两者之间存在链接。

执行以下查询时出错:

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

错误消息显示为:

将表达式转换为数据类型datetime的算术溢出错误。

我注意到我尝试使用的其中一个字段(t1.u_shipdetl)是用户创建的,是否可能没有正确创建?

由于

1 个答案:

答案 0 :(得分:0)

ShipDateU_ShipDetl中的一个不是datetime数据类型,将该转换为日期时间值的尝试失败。 算术溢出错误表明它正在尝试从数字类型转换。也许值存储为20170509?如果是这样,这可能有效:

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