日期:2014-05-18  浏览次数:20621 次

日期前后比较问题?
有个表     字段   mytime
a.     2007-09-02   12:12:12.787
b.     2007-09-03   12:12:12.787
c.     2007-09-07   12:12:12.787
d.     2007-09-21   12:23:34.543

是datatime类型

我现在想取出b,c两条记录         类似   mytime> 2007-09-03   and   mytime <2007-09-21  
正确的怎么写?



------解决方案--------------------
有个表 字段 mytime
a. 2007-09-02 12:12:12.787
b. 2007-09-03 12:12:12.787
c. 2007-09-07 12:12:12.787
d. 2007-09-21 12:23:34.543

是datatime类型

我现在想取出b,c两条记录 类似 mytime> '2007-09-03 ' and mytime < '2007-09-22 '
正确的怎么写?

select * from tb where mytime> '2007-09-03 ' and mytime < '2007-09-22 '
select * from tb where mytime> = '2007-09-03 00:00:00.000 ' and mytime <= '2007-09-21 23:59:59.999 '

------解决方案--------------------
--原始数据:@T1
declare @T1 table(id varchar(2),mytime datetime)
insert @T1
select 'a. ', '2007-09-02 12:12:12.787 ' union all
select 'b. ', '2007-09-03 12:12:12.787 ' union all
select 'c. ', '2007-09-07 12:12:12.787 ' union all
select 'd. ', '2007-09-21 12:23:34.543 '

select * from @T1 where mytime> = '2007-09-03 ' and mytime < '2007-09-21 '

------解决方案--------------------
有个表 字段 mytime
a. 2007-09-02 12:12:12.787
b. 2007-09-03 12:12:12.787
c. 2007-09-07 12:12:12.787
d. 2007-09-21 12:23:34.543

是datatime类型

我现在想取出b,c两条记录 类似 mytime> 2007-09-03 and mytime <2007-09-21
正确的怎么写?

select * from table where mytime> = '2007-09-03 ' and mytime= < '2007-09-21 '