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

一个关于日期查询的简单SQL?
create   table   test
(
id   char(3),
rq   datetime
)
insert   into   test   select   '1 ', '2007-1-1 '
insert   into   test   select   '2 ', '2007-1-1 '
insert   into   test   select   '3 ', '2007-1-2 '
insert   into   test   select   '1 ', '2007-2-1 '
insert   into   test   select   '1 ', '2007-2-2 '
insert   into   test   select   '1 ', '2007-2-3 '
drop   table   test
/*
要求按照年份和月份查询
例如:查询2007年1月份
'1 ', '2007-1-1 '
'2 ', '2007-1-1 '
'3 ', '2007-1-2 '
查询2007年2月份
'1 ', '2007-2-1 '
'1 ', '2007-2-2 '
'1 ', '2007-2-3 '
依次类推。。。
*/

------解决方案--------------------
select * from test
where year(rq) = @年份参数 and month(rq) = @月份参数