读书人

sql查询语句?该怎么解决

发布时间: 2012-02-05 12:07:14 作者: rapoo

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月份的记录
1a1102007-01-01 00:00:00.000
1a1202007-01-02 00:00:00.000
1a1302007-01-03 00:00:00.000

要查询2月份的记录

1a1402007-02-01 00:00:00.000
1a1502007-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月

读书人网 >SQL Server

热点推荐