日期:2014-05-19  浏览次数:20507 次

sql查询语句?
create   table   test
(
id   int,
name   varchar(10),
sl     int,
rq   datetime
)

insert   into   test   select   1, 'a1 ',10, '2007-1-1 '
insert   into   test   select   1, 'a1 ',20, '2007-1-2 '
insert   into   test   select   1, 'a1 ',30, '2007-1-3 '
insert   into   test   select   1, 'a1 ',40, '2007-2-1 '
insert   into   test   select   1, 'a1 ',50, '2007-2-2 '
insert   into   test   select   1, 'a1 ',40, '2007-3-1 '
insert   into   test   select   1, 'a1 ',50, '2007-3-2 '

select   *   from   test
/*
设置一个日期查询条件,查询到指定月份的记录,

比如要查询1月份的记录
1 a1 10 2007-01-01   00:00:00.000
1 a1 20 2007-01-02   00:00:00.000
1 a1 30 2007-01-03   00:00:00.000

要查询2月份的记录

1 a1 40 2007-02-01   00:00:00.000
1 a1 50 2007-02-02   00:00:00.000


*/
drop   table   test

------解决方案--------------------
Select * From test Where Month(rq) = 1

Select * From test Where Month(rq) = 2
------解决方案--------------------
写个存储过程

create proc pr_querytest
@dt datetime
as

select * from test
where rq> =convert(varchar(7),dt,120)+ '-01 '
and rq <dateadd(month,1,convert(varchar(7),dt,120)+ '-01 ')

go

--调用
exec pr_querytest '2007-1-1 ' --查1月
go
exec pr_querytest '2007-2-1 ' --查2月