declare @startdate datetime, @enddate datetime select * from 表名 where 日期字段名>=@startdate and 日期字段名<=@enddate --注意上面的查询对于日期为null的记录是不显示的.
select * from 表 where convert(char(6),日期字段) between '1996-03' and '1997-02'
select * from table where rq between '1996-03-01' and '1997-02-28'
说错了,应该是: select * from 表 where convert(char(7),日期字段,120) between '1996-03' and '1997-02'
sunqi_790817(奇奇) 的方法不行,月份是不固定的。其他两位的方法试过了不行啊
--创建测试环境 create table # ( dt datetime ) insert # select '1996-3-1' union select '1997-2-1' union select '2005-1-1' union select getdate()--测试 select * from # where convert(char(7),dt,120) between '1996-03' and '1997-02'--删除测试环境 drop table #--结果 /* dt ------------------------------------------------------ 1996-03-01 00:00:00.000 1997-02-01 00:00:00.000(所影响的行数为 2 行) */
vivianfdlpw() 的方法可以的,但查出的是不包括1997年2月的数据,而是到1997年1月的数据 改成下面的方法,才可以水能解释一下吗 select * from orders where convert(char(7),orderdate,120)>='1996-10' and convert(char(7),orderdate,120)<='1997-02'
select *
from 表名
where 日期字段名>=@startdate and 日期字段名<=@enddate
--注意上面的查询对于日期为null的记录是不显示的.
select * from 表 where convert(char(7),日期字段,120) between '1996-03' and '1997-02'
create table #
(
dt datetime
)
insert #
select '1996-3-1' union
select '1997-2-1' union
select '2005-1-1' union select getdate()--测试
select * from # where convert(char(7),dt,120) between '1996-03' and '1997-02'--删除测试环境
drop table #--结果
/*
dt
------------------------------------------------------
1996-03-01 00:00:00.000
1997-02-01 00:00:00.000(所影响的行数为 2 行)
*/
改成下面的方法,才可以水能解释一下吗
select * from orders where convert(char(7),orderdate,120)>='1996-10'
and convert(char(7),orderdate,120)<='1997-02'