我有一个先前工作正常的联合查询。但是,如果我现在尝试运行查询,则会出现以下错误
Msg 537,Level 16,State 3,Line 1
传递给LEFT或SUBSTRING函数的长度参数无效。
我已经尝试分别在联合中运行每个单独的查询,它们都工作正常。由于这两个查询都独立于联合工作,它似乎表明问题是一个工会问题,但由于它以前工作没有改变工会,它似乎表明数据问题,所以我不知道接下来做什么解决问题。
select month([UD03].[Date02]) as 'Month',
datename(month,[UD03].[Date02]) as 'Month Name',
case
when ud03.key1=' ' then ud03.key2+ud03.key3+ud03.key4
when ud03.key2=' ' then ud03.key1+ud03.key3+ud03.key4
end,
Case
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) < 10 then 'Under 10"'
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 10 and 19.999 then '10-19.999"'
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 20 and 29.999 then '20-29.999"'
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 30 and 39.999 then '30-39.999"'
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 40 and 49.999 then '40-49.999"'
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 50 and 59.999 then '50-59.999"'
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 60 and 69.999 then '60-69.999"'
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 70 and 79.999 then '70-79.999"'
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 80 and 89.999 then '80-89.999"'
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 90 and 99.999 then '90-99.999"'
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) >=100.000 then '100.000"+'
end as 'OD Range',
Case
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) < 10 then 1
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 10 and 19.999 then 2
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 20 and 29.999 then 3
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 30 and 39.999 then 4
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 40 and 49.999 then 5
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 50 and 59.999 then 6
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 60 and 69.999 then 7
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 70 and 79.999 then 8
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 80 and 89.999 then 9
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) between 90 and 99.999 then 10
when CHARINDEX('x', ud03.ShortChar02,1) >0 and try_cast(SUBSTRING(ud03.shortchar02,1,CHARINDEX('x', ud03.ShortChar02,1)-1) as decimal(6,3)) >=100.000 then 11
end as 'Sort Order',
'Quotes' as 'Type',
Case
when [UD03].[CheckBox05]=0 and [UD03].[CheckBox06]=0 and [UD03].[CheckBox13]=0 then '63/63'
when [UD03].[CheckBox06]=1 then '160/160'
end as 'Mill'
from Ice.UD03 as UD03
inner join Ice.UD02 as UD02 on
UD03.Company = UD02.Company
And
CAST(CAST(UD03.Number09 AS INT) AS VARCHAR(30)) = UD02.Key1
left outer join Erp.Customer as Customer on
UD03.Company = Customer.Company
And
UD03.Key1 = Customer.Name
left outer join Erp.SalesTer as SalesTer on
Customer.Company = SalesTer.Company
And
Customer.TerritoryID = SalesTer.TerritoryID
left outer join Erp.CustGrup as CustGrup on
Customer.Company = CustGrup.Company
And
Customer.GroupCode = CustGrup.GroupCode
where (UD03.Key3 <> '0') and ([UD03].[Date02]>=CONVERT(datetime,'01/01/'+CONVERT(VARCHAR(4), DATEPART(yy,@StartDate))) and [UD03].[Date02]<=CONVERT(datetime,'12/31/'+CONVERT(VARCHAR(4), DATEPART(yy,@StartDate)))) and (([UD03].[CheckBox05]=0 and [UD03].[CheckBox06]=0 and [UD03].[CheckBox13]=0 and (ud03.key1 not like 'Crossett ASH%' and ud03.key1 not like 'Crossett TY%')) or [UD03].[CheckBox06]=1) and ([UD02].[ShortChar01]='Allo' or [UD02].[ShortChar01]='Carb' or [UD02].[ShortChar01]='S300' or [UD02].[ShortChar01]='S400') and (([UD03].[CheckBox05]=0 and [UD03].[CheckBox06]=0 and [UD03].[CheckBox13]=0) or ([UD03].[CheckBox06]=1)) and ud03.key1 not like 'Zurn%'
union all
select month([orderhed].OrderDate) as 'Month',
datename(month,[orderhed].OrderDate) as 'Month Name',
cast(cast(orderhed.ordernum as nvarchar) + cast(orderdtl.orderline as nvarchar) as nvarchar) as 'Ordernumber and Line',
Case
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3)) < 10 then 'Under 10"'
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3)) between 10 and 19.999 then '10-19.999"'
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 20 and 29.999 then '20-29.999"'
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 30 and 39.999 then '30-39.999"'
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 40 and 49.999 then '40-49.999"'
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 50 and 59.999 then '50-59.999"'
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 60 and 69.999 then '60-69.999"'
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 70 and 79.999 then '70-79.999"'
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 80 and 89.999 then '80-89.999"'
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 90 and 99.999 then '90-99.999"'
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))>=100.000 then '100.000"+'
end as 'OD Range',
Case
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3)) < 10 then 1
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3)) between 10 and 19.999 then 2
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 20 and 29.999 then 3
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 30 and 39.999 then 4
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 40 and 49.999 then 5
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 50 and 59.999 then 6
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 60 and 69.999 then 7
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 70 and 79.999 then 8
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 80 and 89.999 then 9
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))between 90 and 99.999 then 10
when try_cast(SUBSTRING(orderdtl.linedesc, CHARINDEX('- ', orderdtl.linedesc,1)+2,
(CHARINDEX('x',orderdtl.linedesc,1)-2)-CHARINDEX('- ', orderdtl.linedesc,1)) as decimal(6,3))>=100.000 then 11
end as 'Sort Order',
'Orders' as 'Type',
case
when [OrderDtl].[ProdCode] Like '63%' then '63/63'
when [OrderDtl].[ProdCode] Like '160%' then '160/160'
end as 'Mill'
from Erp.OrderHed as OrderHed
inner join Erp.Customer as Customer on
OrderHed.Company = Customer.Company
And
OrderHed.CustNum = Customer.CustNum
inner join Erp.OrderDtl as OrderDtl on
OrderHed.Company = OrderDtl.Company
And
OrderHed.OrderNum = OrderDtl.OrderNum
left outer join Erp.SalesCat as SalesCat on
SalesCat.Company = OrderDtl.Company
And
SalesCat.SalesCatID = OrderDtl.SalesCatID
where orderhed.OrderDate >=CONVERT(datetime,'01/01/'+CONVERT(VARCHAR(4), DATEPART(yy,@StartDate))) and orderhed.OrderDate <=CONVERT(datetime,'12/31/'+CONVERT(VARCHAR(4), DATEPART(yy,@StartDate))) and [OrderDtl].[ProdCode] <> 'KFRR' and [SalesCat].[Description] <> 'KFR630 Rings' and [SalesCat].[Description] <> 'Other' and orderhed.custnum <>942 and orderhed.custnum<>725 and customer.custnum <> 1086