if object_id('[tabletest]') is not null drop table [tbl]
create table [tabletest]([DATime] datetime,[DaWeight] varchar(5))
insert [tabletest]
select '2012-03-15 14:24:00.000','300Kg' union all
select '2012-03-15 14:25:00.000','400Kg'
go
declare @datimenow DateTime
set @datimenow=getdate()
select * from tabletest
--直接取最新的记录
select top 1 * from tabletest order by DATime desc
--取小于当前时间的最新的记录
select top 1 * from tabletest where DATime < @datimenow order by DATime desc
--如果是取离当前时间最近的,包括大于当前时间,按照秒来算.
--如果只有一条记录.
select top 1 * from tabletest order by abs(datediff(ss,datime,@datimenow))
--如果可能存在多条记录.
select * from tabletest where abs(datediff(ss,datime,@datimenow)) = (select min(abs(datediff(ss,datime,@datimenow))) from tabletest)
--根据差值排序,取序列号。根据指定序列号返回数据。
select * from(
select ROW_NUMBER()over(order by datediff(mi,datime,getdate())) as id,
* from tabletest) a where id=1