select * from a where a.datetime>convert(datetime,@最小时间) and a.datetime<convert(datetime,@最大时间)
declare @t table(id int identity,[datetime] datetime) insert @t select dateadd(d,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate()) union all select dateadd(day,rand()*(-1000),getdate())select * from @t id datetime ----------- ----------------------- 1 2007-10-31 09:14:06.043 2 2009-03-21 09:14:06.043 3 2010-03-30 09:14:06.043 4 2009-04-25 09:14:06.043 5 2009-03-04 09:14:06.043 6 2008-07-15 09:14:06.043 7 2008-07-27 09:14:06.043 8 2008-10-03 09:14:06.043 9 2008-02-06 09:14:06.043 10 2010-05-10 09:14:06.043 11 2008-12-24 09:14:06.043 12 2010-01-20 09:14:06.043 13 2009-03-21 09:14:06.043 14 2007-10-22 09:14:06.043(14 行受影响)declare @maxDate DateTime declare @minDate DateTime select @maxDate='2010-01-01',@minDate='2007-01-01'select * from @t where datetime between @minDate and @maxDateid datetime ----------- ----------------------- 1 2007-10-31 09:14:06.043 2 2009-03-21 09:14:06.043 4 2009-04-25 09:14:06.043 5 2009-03-04 09:14:06.043 6 2008-07-15 09:14:06.043 7 2008-07-27 09:14:06.043 8 2008-10-03 09:14:06.043 9 2008-02-06 09:14:06.043 11 2008-12-24 09:14:06.043 13 2009-03-21 09:14:06.043 14 2007-10-22 09:14:06.043(11 行受影响)
declare @beginTime datetime declare @endTime datetime set @begintTime=getdate() set @endTime='2009-1-1'
declare @Date DateTime 定义变量可以 但是你既然在c#这边提问,你语句不能到页面上写?DateTime MinTime='2010-01-01'; DateTime MaxTime='2010-07-04'; string sql="select * from a where a.datetime>'"+MinTime.Tostring()+"' and a.datetime<'"+MaxTime.Tostring()+"';"
select * from a where a.datetime>@MIN_DATETIME and a.datetime<@MAX_DATETIME
declare endTime datetimeselect * from a where a.datetime>startTime and a.datetime<endTime
declare endTime datetime
declare @t table(id int identity,[datetime] datetime)
insert @t
select dateadd(d,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate()) union all
select dateadd(day,rand()*(-1000),getdate())select * from @t
id datetime
----------- -----------------------
1 2007-10-31 09:14:06.043
2 2009-03-21 09:14:06.043
3 2010-03-30 09:14:06.043
4 2009-04-25 09:14:06.043
5 2009-03-04 09:14:06.043
6 2008-07-15 09:14:06.043
7 2008-07-27 09:14:06.043
8 2008-10-03 09:14:06.043
9 2008-02-06 09:14:06.043
10 2010-05-10 09:14:06.043
11 2008-12-24 09:14:06.043
12 2010-01-20 09:14:06.043
13 2009-03-21 09:14:06.043
14 2007-10-22 09:14:06.043(14 行受影响)declare @maxDate DateTime
declare @minDate DateTime
select @maxDate='2010-01-01',@minDate='2007-01-01'select * from @t where datetime between @minDate and @maxDateid datetime
----------- -----------------------
1 2007-10-31 09:14:06.043
2 2009-03-21 09:14:06.043
4 2009-04-25 09:14:06.043
5 2009-03-04 09:14:06.043
6 2008-07-15 09:14:06.043
7 2008-07-27 09:14:06.043
8 2008-10-03 09:14:06.043
9 2008-02-06 09:14:06.043
11 2008-12-24 09:14:06.043
13 2009-03-21 09:14:06.043
14 2007-10-22 09:14:06.043(11 行受影响)
declare @endTime datetime
set @begintTime=getdate()
set @endTime='2009-1-1'
大于最小的,小于最大的,不就是所有的吗?(当然除去未知的)
定义变量可以
但是你既然在c#这边提问,你语句不能到页面上写?DateTime MinTime='2010-01-01';
DateTime MaxTime='2010-07-04';
string sql="select * from a where a.datetime>'"+MinTime.Tostring()+"' and a.datetime<'"+MaxTime.Tostring()+"';"