这里when是用在哪里的?会不会式 when 的错误,改为 wherewhen datediff([n],starttime,getdate()) % spatime=0 and datediff([n],starttime,getdate())<>0
解决方案 »
- 2008能审核出某用户进行了何种类型的操作吗?
- 从一个表插入另一个表出现错误,请前辈指点!
- 还是关于连续数据判断的问题
- 或多或少,谈谈sysbase
- 求教一个聚合统计的语句
- sql server 存储过程 where 后面加判断出错怎么办?
- 触发器问题,关于inserted表,和deleted表
- 请大家看看这个问题,100分请大家帮忙
- &&&&&&&&&&&&&&一个简单的SQL语句问题&&&&&&&&&&&& 早上结帐
- 如何从win98+sql7的数据库中把数据倒到win2000p+sql2000中,二者异构,要求实时更新目标库
- access中有没有包括null值在内的通配符?
- 请各位老师帮忙看看如何 处理 谢谢拉
sqltime = "update fenji Set Flags = b.Flag from fenji a inner join ( select pointname,flag=case when (datediff( [n],starttime,getdate()) % spatime =1) then 1 else 0 end From ( select a.pointname,a.starttime,convert(datetime,max(b.xdate+' '+b.xtime),120) as xtime,a.spatime from fenji a inner join xinxi b on a.pointname=b.pointname and a.starttime<getdate() group by a.pointname,a.starttime,a.spatime )t )b on a.PointName=b.PointName"如果我加上where datediff([n],starttime,getdate())<>0 总是会报错
create table #t (DT datetime,DES varchar(10))insert into #t values('2005-07-24','7-24')
insert into #t values('2005-07-25','7-25')select * from #t
DT DES
------------------------------------------------------ ----------
2005-07-24 00:00:00.000 7-24
2005-07-25 00:00:00.000 7-25(2 row(s) affected)
select 'Day' =
case when datediff([n],dt,datediff(n,'2005-07-25 00:00:00.000',convert(varchar(20),convert(datetime,convert(varchar(10),getdate(),120)),120))) % 3600 = 0
and datediff([n],dt,datediff(n,'2005-07-25 00:00:00.000',convert(varchar(20),convert(datetime,convert(varchar(10),getdate(),120)),120))) <> 0 then 'today'
else 'the other day'
end,des,datediff([n],dt,getdate()),dt
from #t
/*
Day des dt
------------- ---------- ----------- ------------------------------------------------------
today 7-24 2364 2005-07-24 00:00:00.000
the other day 7-25 924 2005-07-25 00:00:00.000(2 row(s) affected)
*/从我的这个测试数据,你也应该看到问题,你用datadiff是计算相差的分钟,你用getdate()来比较,每过一分钟,你的结果就不一样,你看:
print datediff(n,'2005-07-25 00:00:00.000',convert(varchar(20),convert(datetime,convert(varchar(10),getdate(),120)),120))
print datediff(n,'2005-07-25 00:00:00.000',getdate())
print convert(varchar(20),getdate(),120)
/*
0
929
2005-07-25 15:29:06
*/
第一个只是取出今天的年月日来和'2005-07-25 00:00:00.000'比较,是相等,但是你用getdate()来比较,就是不一样的,所以你用的when datediff([n],starttime,getdate()) % spatime=0 and datediff([n],starttime,getdate())<>0肯定是有起作用,
你用case ....当然是搭配用when
应该是
flag=case when (datediff( [n],starttime,getdate()) % spatime =1 --不是说余等于0吗
) then 1 else 0 end
改为
flag=case when (datediff( [n],starttime,getdate()) % spatime =0
and datediff([n],starttime,getdate())<>0 ) then 1
else 0 end ---你原来用的就是对的
你要check你的数据,到底相等吗?你用的是分钟比较,例如
print datediff(n,'2005-07-25 00:00:00.000',convert(varchar(20),convert(datetime,convert(varchar(10),getdate(),120)),120))
print datediff(n,'2005-07-25 00:00:00.000',getdate())
结果是不一样的,你要自己算算到底相等不?
getdate()每过一分钟datadiff就不一样,你的语句有陷阱~
我试过用flag=case when (datediff( [n],starttime,getdate()) % spatime =0
and datediff([n],starttime,getdate())<>0 ) then 1 else 0 end ---但是结果和没有修改前是一样的,没有变化
原来的贴子地址是http://community.csdn.net/Expert/topic/4056/4056872.xml?temp=.7971918其中第二个问题,如果可以的话请你去看一下,可能有助于问题的解决,或者抛开我现在的这种写法,有没有别的解决办法?
原来是打算取余等于0的,但是经过一段时间的思考后觉得还是=1比较好
case when a=b and c=d then,我看了很多贴子都是只有一个条件
when datediff([n],starttime,getdate()) % spatime=0 and datediff([n],starttime,getdate())<>0 ,还有一个小问题,应该把 and 后面的条件放在前面