日期:2014-05-20 浏览次数:21077 次
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能处理的问题 非要搬到代码里面去执行
按照你这需求 还不如弄个存储过程