注册 登录
编程论坛 SQL Server论坛

有关语句有错..

qq402716710 发布于 2013-01-24 14:02, 435 次点击
select  ch,gname,cx,lxr,lxrtel,logdate,cyear
from tbxlclzl
where datediff(month,logdate,getdate()+5)%6=0
and
case when month(dateadd(month,datediff(month,logdate,getdate()+5),logdate)+5)>
month(dateadd(month,datediff(month,logdate,getdate()+5),logdate))
then
datediff(day,getdate()+5,dateadd(month,datediff(month,logdate,getdate()+5),logdate))
between -5
and 0
else
datediff(day,getdate(),dateadd(month,datediff(month,logdate,getdate()+5),logdate))
between 0
and 5
end
and color='货车'
and awycens = 1

提示between附近有错,但是我把between上面的datediff(day,getdate()+5,dateadd(month,datediff(month,logdate,getdate()+5),logdate))
放到select随便找条件查询都能得出一个数值,为什么between的时候就不行呢..求解

4 回复
#2
qq4027167102013-01-24 15:11
select  ch,gname,cx,lxr,lxrtel,logdate,cyear
from tbxlclzl
where datediff(month,logdate,getdate()+5)%6=0
and
month(dateadd(month,datediff(month,logdate,getdate()+5),logdate)+5)>
month(dateadd(month,datediff(month,logdate,getdate()+5),logdate))
and color='货车'
and awycens = 1
and
datediff(day,getdate()+5,dateadd(month,datediff(month,logdate,getdate()+5),logdate))
between -5
and 0
union all
select  ch,gname,cx,lxr,lxrtel,logdate,cyear
from tbxlclzl
where datediff(month,logdate,getdate()+5)%6=0
and
month(dateadd(month,datediff(month,logdate,getdate()+5),logdate)+5)=
month(dateadd(month,datediff(month,logdate,getdate()+5),logdate))
and
datediff(day,getdate(),dateadd(month,datediff(month,logdate,getdate()+5),logdate))
between 0
and 5
and color='货车'
and awycens = 1
改成这样就可以了..我晕
#3
qq4027167102013-01-24 15:55
知道是为什么吗?为什么条件中不能使用CASE when 语句
#4
xiaolee2013-03-15 11:40
表示看不懂
#5
Aviva_Wang2013-03-15 13:05
你的case是放where中了,当然不可以了呀
1