解决sql中的算术溢出错误?

解决sql中的算术溢出错误?,sql,sql-server,Sql,Sql Server,我试图运行以下查询,但一直返回错误 (select distinct a.no_,a.rowno,a.rowno_glglhi_gl,a.amount as amount,a.effective ,a.branch from ( --gls from the last 40 days select ks208.dbo.gl.no_,ks208.dbo.glhi.rowno,ks208.dbo.glhi.rowno_glglhi_gl,ks208.dbo.glhi.amount,ks208.db

我试图运行以下查询,但一直返回错误

(select distinct a.no_,a.rowno,a.rowno_glglhi_gl,a.amount as amount,a.effective ,a.branch from
(
--gls from the last 40 days 
select ks208.dbo.gl.no_,ks208.dbo.glhi.rowno,ks208.dbo.glhi.rowno_glglhi_gl,ks208.dbo.glhi.amount,ks208.dbo.glhi.effective,ks208.dbo.gl.branch
from ks208.dbo.glhi
left join ks208.dbo.gl on ks208.dbo.gl.rowno=ks208.dbo.glhi.rowno_glglhi_gl

union 
--gls from the larger database
select dmon208.dbo.gl.no_,dmon208.dbo.glhi.rowno,dmon208.dbo.glhi.rowno_glglhi_gl,dmon208.dbo.glhi.amount ,dmon208.dbo.glhi.effective,dmon208.dbo.gl.branch
from dmon208.dbo.glhi
left join DMON208.dbo.gl on DMON208.dbo.gl.rowno=dmon208.dbo.glhi.rowno_glglhi_gl) as a

union

--gls with no glhi
select extra.no_,extra.rowno,extra.rowno_glglhi_gl,extra.amount,extra.effective,extra.branch from
(select distinct gl.no_,gl.rowno,dateadd(year,-10,getdate()) as 'effective',0.00 as 'amount',gl.branch,gl.rowno as rowno_glglhi_gl from gl 
left join glhi on glhi.rowno_glglhi_gl = gl.rowno
where gl.status != 'closed'
and gl.no_ not in( select distinct gl.no_ from gl
inner join glhi on glhi.rowno_glglhi_gl = gl.rowno 
where gl.status != 'closed')

union 

select distinct gl.no_,gl.rowno,dateadd(day,-1,getdate()) as 'effective',0.00 as 'amount',gl.branch,gl.rowno as rowno_glglhi_gl from gl 
left join glhi on glhi.rowno_glglhi_gl = gl.rowno
where gl.status != 'closed'
and gl.no_ not in( select distinct gl.no_ from gl
inner join glhi on glhi.rowno_glglhi_gl = gl.rowno 
where gl.status != 'closed')) as extra

union 

select gl.no_,gl.rowno,dateadd(day,-1,getdate()) as 'effective',0.00 as 'amount',gl.branch,gl.rowno as rowno_glglhi_gl from gl 
left join glhi on glhi.rowno_glglhi_gl = gl.rowno
where glhi.rowno_glglhi_gl
in(
(-- gl's not updated yesterday
select distinct glhi.rowno_glglhi_gl from glhi where glhi.rowno_glglhi_gl
not in (
--gl's updated as of yesterday
select distinct glhi.rowno_glglhi_gl from glhi where
glhi.effective = cast(convert(char(11), dateadd(day,-1, getdate()), 113) as datetime))))


) as final 

where year(final.effective) >= year(getdate())-4
group by final.no_,final.effective,final.branch,final.rowno_glglhi_gl

order by final.no_,final.rowno_glglhi_gl,final.effective desc
Msg 8115,16级,状态2,第1行
将表达式转换为数据类型datetime时出现算术溢出错误

问题在于

left join glhi on glhi.rowno_glglhi_gl = gl.rowno
where glhi.rowno_glglhi_gl
in(
(
select distinct glhi.rowno_glglhi_gl from glhi where glhi.rowno_glglhi_gl
not in (
select distinct glhi.rowno_glglhi_gl from glhi where
glhi.effective = cast(convert(char(11), dateadd(day,-1, getdate()), 113) as datetime))))
出于某种原因,它本身运行良好,但不是作为更大查询的一部分

我试过:

select cast(convert(char(11), dateadd(day,-1, getdate()), 113) as datetime)

select DATEADD(dd, DATEDIFF(dd, 1, getdate()), 0)

select CONVERT(DATE, dateadd(day,-1, getdate()), 101) 

为什么要将日期/时间转换为字符串,然后再转换回日期,只是为了删除时间组件

我怀疑你想要:

glhi.effective = convert(date, dateadd(day, -1, getdate()))

比较
date
datetime
应该可以。

由于某些原因,glhi.effective似乎与dateadd(day,-1,getdate())不兼容。如果我将其用作筛选器,则它不会检索任何结果。@DominicNaimool。嗯,你的代码也不起作用。也许您需要:
convert(date,glhi.effective)=convert(date,dateadd(day,-1,getdate())
。如果没有样本数据和期望的结果,只有您才能找到答案。