Sql 将nvarchar值“some name”转换为数据类型int时,转换失败。

Sql 将nvarchar值“some name”转换为数据类型int时,转换失败。,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,当我运行整个查询时,会收到以下错误消息: ------------------------------------ --POPULATE SECTIONAAVGTAT----------- ------------------------------------ ;with SpecimenDetail as ( select s.* ,a.[mlis id] ,a.director ,a.rm ,a.rep ,a.css ,a.css2 ,dbo.networkdays(s.[date rec

当我运行整个查询时,会收到以下错误消息:

------------------------------------
--POPULATE SECTIONAAVGTAT-----------
------------------------------------
;with SpecimenDetail as (
select
s.*
,a.[mlis id]
,a.director
,a.rm
,a.rep
,a.css
,a.css2
,dbo.networkdays(s.[date received],GETDATE())-1 [Days On Hold]
,(case when dbo.networkdays(s.[date received],GETDATE())-1>=7 then '7+' 
   when dbo.networkdays(s.[date received],GETDATE())-1 in (5,6) then '5-6'
   when dbo.networkdays(s.[date received],GETDATE())-1 in (3,4) then '3-4'
   when dbo.networkdays(s.[date received],GETDATE())-1 in (1,2) then '1-2'
   when dbo.networkdays(s.[date received],GETDATE())-1=0 then '0'
  else 'na'
end) as [Days on Hold Group]
 from specimendetailtmp s
left join sectionaalignment a
on s.[mlis practice id]=a.[mlis id]
where s.[date distributed] is not null
and s.[date received]>='20121112'
and s.sectiona='not marked'
)


,

AvgTAT as
(
select 'director' emptype,director employee, cast(round(AVG(cast (dbo.networkdays(s.[date received],s.[date distributed])-1  as float)), 3, 1) as decimal(10,1)) AvgTAT,month(s.[date received]) month from SpecimenDetail s
where s.[date distributed] is not null
group by director,month(s.[date received])


union all

select 'rm' emptype,rm employee, cast(round(AVG(cast (dbo.networkdays(s.[date received],s.[date distributed])-1  as float)), 3, 1) as decimal(10,1))AvgTAT,month(s.[date received]) month from SpecimenDetail s
where s.[date distributed] is not null
group by rm,month(s.[date received])

union all

select 'rep' emptype,rep employee,cast(round(AVG(cast (dbo.networkdays(s.[date received],s.[date distributed])-1 as float)), 3, 1) as decimal(10,1)) AvgTAT,month(s.[date received]) month from SpecimenDetail s
where s.[date distributed] is not null
group by rep,month(s.[date received])

union all

select 'css' emptype,css employee,cast(round(AVG(cast (dbo.networkdays(s.[date received],s.[date distributed])-1  as float)), 3, 1) as decimal(10,1)) AvgTAT,month(s.[date received]) month from SpecimenDetail s
where s.[date distributed] is not null
group by css,month(s.[date received])

union all

select 'css2' emptype,css2 employee,cast(round(AVG(cast (dbo.networkdays(s.[date received],s.[date distributed])-1  as float)), 3, 1) as decimal(10,1)) AvgTAT,month(s.[date received]) month from SpecimenDetail s
where s.[date distributed] is not null
group by css2,month(s.[date received])
)

select * from avgtat
然而,如果我运行任何一个内部选择,一切都很好


为什么只有在执行整个脚本时才会出现此错误?

我怀疑UNION ALL segments中的一个或多个SELECT子句正在生成隐式转换为int的结果。而其他SELECT子句返回的结果是nvarchar。当结果中的列值都是整数时,可能会发生隐式转换


如果您不知道是哪一列导致了问题,则应查看每个SELECT的结果,并确定是否存在共同模式,例如,nvarchar列中的所有值都是整数。然后,当您知道产生问题的列时,使用CAST或CONVERT将值显式转换为所需的数据类型。

非常感谢!但我甚至不知道它引用的是哪一列!首先,可以从公共表表达式中删除未使用的列。并且,暂时从UNION ALL查询中删除列,直到错误消失。删除的最后一列是导致问题的列。