--求2004-3-19 1:03時的data select data from 表 where convert(varchar(16),時間,120)='2004-03-19 01:03' --求2004-3-19 1點時的data select data from 表 where convert(varchar(13),時間,120)='2004-03-19 01'
--测试--测试数据 create table 表(id int,时间 datetime,data int) insert 表 select 1,'2004-3-19 1:01:00',200 union all select 2,'2004-3-19 1:02:00',400 union all select 3,'2004-3-19 1:03:00',300 union all select 4,'2004-3-19 1:04:00',800 union all select 5,'2004-3-19 2:00:00',700 union all select 6,'2004-3-19 2:01:00',700 union all select 7,'2004-3-19 2:02:00',200 go--按分钟 select * from 表 a where 时间=( select min(时间) from 表 where 时间>=convert(varchar(16),a.时间,120))--按小时 select * from 表 a where 时间=( select min(时间) from 表 where 时间>=convert(varchar(14),a.时间,120)+'00') go--删除测试环境 drop table 表/*--测试结果 id 时间 data ----------- ------------------------------------------------------ ----------- 1 2004-03-19 01:01:00.000 200 2 2004-03-19 01:02:00.000 400 3 2004-03-19 01:03:00.000 300 4 2004-03-19 01:04:00.000 800 5 2004-03-19 02:00:00.000 700 6 2004-03-19 02:01:00.000 700 7 2004-03-19 02:02:00.000 200(所影响的行数为 7 行)id 时间 data ----------- ------------------------------------------------------ ----------- 1 2004-03-19 01:01:00.000 200 5 2004-03-19 02:00:00.000 700(所影响的行数为 2 行)--*/
select data from 表 where convert(varchar(16),時間,120)='2004-03-19 01:03'
--求2004-3-19 1點時的data
select data from 表 where convert(varchar(13),時間,120)='2004-03-19 01'
create table 表(id int,时间 datetime,data int)
insert 表 select 1,'2004-3-19 1:01:00',200
union all select 2,'2004-3-19 1:02:00',400
union all select 3,'2004-3-19 1:03:00',300
union all select 4,'2004-3-19 1:04:00',800
union all select 5,'2004-3-19 2:00:00',700
union all select 6,'2004-3-19 2:01:00',700
union all select 7,'2004-3-19 2:02:00',200
go--按分钟
select * from 表 a
where 时间=(
select min(时间) from 表
where 时间>=convert(varchar(16),a.时间,120))--按小时
select * from 表 a
where 时间=(
select min(时间) from 表
where 时间>=convert(varchar(14),a.时间,120)+'00')
go--删除测试环境
drop table 表/*--测试结果
id 时间 data
----------- ------------------------------------------------------ -----------
1 2004-03-19 01:01:00.000 200
2 2004-03-19 01:02:00.000 400
3 2004-03-19 01:03:00.000 300
4 2004-03-19 01:04:00.000 800
5 2004-03-19 02:00:00.000 700
6 2004-03-19 02:01:00.000 700
7 2004-03-19 02:02:00.000 200(所影响的行数为 7 行)id 时间 data
----------- ------------------------------------------------------ -----------
1 2004-03-19 01:01:00.000 200
5 2004-03-19 02:00:00.000 700(所影响的行数为 2 行)--*/