2009-10-12 08:00:00
2009-10-12 08:30:00
2009-10-12 17:59:00三个时间一条语句,取大于 2009-10-12 8:20:00 的最小时间(8:30:00),如果有小于8:20:00的时间,将显示null
2009-10-12 08:30:00
2009-10-12 17:59:00三个时间一条语句,取大于 2009-10-12 8:20:00 的最小时间(8:30:00),如果有小于8:20:00的时间,将显示null
调试欢乐多
print 'null'elseselect top 1 time from tb where time>'2009-10-12 8:20:00' order by time asc
insert into tb values('2009-10-12 08:00:00')
insert into tb values('2009-10-12 08:30:00')
insert into tb values('2009-10-12 17:59:00')
declare @dt as datetime
set @dt = '2009-10-12 08:20:00'if exists(select 1 from tb where dt < @dt)
print 'null'
else
select top 1 dt from tb where dt > @dt order by dtdrop table tbgo/*
null
*/
if object_id('ta') is not null drop table ta
go
create table ta(sj datetime)
insert into ta select
'2009-10-12 08:00:00' union all select
'2009-10-12 08:30:00' union all select
'2009-10-12 17:59:00'
select distinct
case when (select min(sj) from ta)< '2009-10-12 08:20:00' then null
else (select min(sj) from ta where sj>'2009-10-12 08:20:00')
end
from ta
/*
-----------------------
NULL(1 行受影响)
*/
又要显示最小时间、又要显示NULL,又要一条语句?按照你的记录,最小时间肯定只有一条记录啊,怎么又要显示NULL ?
insert into ta select
'2009-10-12 08:00:00' union all select
'2009-10-12 08:30:00' union all select
'2009-10-12 17:59:00' --要么显示null,要么显示'2009-10-12 8:30:00'
select
(case when sj<>'2009-10-12 8:30:00' then null
else sj end)
from ta
--要么显示null,要么显示'2009-10-12 8:30:00'
select
(case when sj=(select min(sj) from ta where sj>'2009-10-12 8:20:00') then sj
else null end)
from ta
select min(time) as time from t_dakarecord
) a
group by time having time>'2009-10-12 8:20:00'
insert into ta select
'2009-10-12 08:00:00' union all select
'2009-10-12 08:30:00' union all select
'2009-10-12 17:59:00' --如果有小于8:20:00的时间,将显示null
--大于2009-10-12 8:20:00的最小时间
select
(case when sj=(select min(sj) from ta where sj>'2009-10-12 8:20:00') then sj
else null end) '大于2009-10-12 8:20:00的最小时间'
from ta--大于8:20:00的最小时间,不管日期是哪一天
select
(case when sj=(select min(sj) from ta where convert(varchar(8),sj,8)>'08:20:00') then sj
else null end) '大于8:20:00的最小时间'
from ta
select min(time) as time from t_dakarecord
) a
group by time having time>'2009-10-12 8:20:00'
--好个屁,要这么多子查询干嘛呢?(结果也不对:无返回记录行)
select min(time) as time from t_dakarecord
where time>'2009-10-12 8:20:00'
--这样还差不多