日期:2014-05-20  浏览次数:21057 次

RowFilter日期筛选请教~!
数据字段类型是“yyyy-MM-dd HH:mm:ss”我想筛选出日期从DAY1天到DAY2天,并且时间在TIME1到TIME2之间的所有数据
例:日期是从2012-04-01至2012-04-10并且时间为08:00:00至09:00:00 的所有数据。请问RowFilter的筛选语句如何写?

------解决方案--------------------
C# code

create table testDate
(
 ID int primary key identity(1,1),
 DateLoan datetime
)
insert into testDate values('2012-4-27 07:20:10')
insert into testDate values('2012-4-28 07:20:10')
insert into testDate values('2012-4-28 08:20:10')
insert into testDate values('2012-4-28 08:30:10')
insert into testDate values('2012-4-28 08:40:10')
insert into testDate values('2012-4-28 08:50:10')
insert into testDate values('2012-4-28 09:00:00')
insert into testDate values('2012-4-28 09:20:10')
insert into testDate values('2012-4-28 09:20:10')

SELECT * FROM testDate
/*
ID          DateLoan
----------- -----------------------
1           2012-04-28 07:20:10.000
2           2012-04-28 08:20:10.000
3           2012-04-28 08:30:10.000
4           2012-04-28 08:40:10.000
5           2012-04-28 08:50:10.000
6           2012-04-28 09:00:00.000
7           2012-04-28 09:20:10.000
8           2012-04-28 09:20:10.000
9           2012-04-27 07:20:10.000

(9 行受影响)
*/
SELECT * FROM testDate WHERE SUBSTRING(CONVERT(NVARCHAR(20),DateLoan,120),1,10) between '2012-04-27' and '2012-04-28' and SUBSTRING(CONVERT(NVARCHAR(20),DateLoan,114),1,8) between '08:00:00' and '09:00:00' 
/*
ID          DateLoan
----------- -----------------------
2           2012-04-28 08:20:10.000
3           2012-04-28 08:30:10.000
4           2012-04-28 08:40:10.000
5           2012-04-28 08:50:10.000
6           2012-04-28 09:00:00.000

(5 行受影响)
*/

------解决方案--------------------
好纠结的人儿。。。
 SQL能处理的问题 非要搬到代码里面去执行
 按照你这需求 还不如弄个存储过程