为@Mikhail +1,
sqlfiddle上的示例。
架构:
create table Tbl_Transactions (
ID int not null
,Date varchar(8) not null
,Hour int
,Minute int
,Location nvarchar(100)
,CardID int
)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (1,'20130101',1,1,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (2,'20130101',0,2,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (3,'20130201',1,1,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (4,'20130101',20,10,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (5,'20130203',1,1,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (6,'20130101',1,11,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (7,'20130102',0,1,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (8,'20121231',23,59,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (9,'20121231',23,50,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (10,'20130101',1,3,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (11,'20130101',1,5,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (12,'20130301',3,1,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (13,'20130401',1,1,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (14,'20130101',1,1,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (15,'20130101',1,2,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (16,'20130201',1,1,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (17,'20130101',20,10,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (18,'20130203',1,1,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (19,'20130101',1,11,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (20,'20130102',0,1,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (21,'20121231',23,59,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (22,'20121231',23,50,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (23,'20130101',1,3,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (24,'20130101',1,5,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (25,'20130301',1,1,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (26,'20130401',1,1,'b',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (27,'20130101',1,1,'a',123456)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (28,'20130101',0,2,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (29,'20130201',1,1,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (30,'20130101',20,10,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (31,'20130203',1,1,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (32,'20130101',1,1,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (33,'20130102',1,1,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (34,'20121231',23,59,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (35,'20121231',23,50,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (36,'20130101',1,3,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (37,'20130101',1,5,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (38,'20130301',1,1,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (39,'20130401',1,1,'a',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (40,'20130101',1,1,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (41,'20130101',1,2,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (42,'20130201',1,1,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (43,'20130101',20,10,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (44,'20130203',1,1,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (45,'20130101',1,1,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (46,'20130102',1,1,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (47,'20121231',23,59,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (48,'20121231',23,50,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (49,'20130101',1,3,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (50,'20130101',1,5,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (51,'20130301',1,1,'b',123457)
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (52,'20130401',1,1,'b',123457)
询问
declare @picktime datetime= cast('2013/01/01 00:01:00' as datetime)
SELECT
*,
cast(
Left(Date,4)
+ '/'
+ Substring(Date,5,2)
+ '/'
+ Right(Date,2)
+ ' '
+ Cast( Hour as varchar)
+ ':'
+ Cast( Minute as varchar)
+ ':00' as datetime)
, Dateadd(mi, -5,@picktime), Dateadd(mi, 5, @picktime)
FROM
Tbl_Transactions
where
(
cast(
Left(Date,4)
+ '/'
+ Substring(Date,5,2)
+ '/'
+ Right(Date,2)
+ ' '
+ Cast( Hour as varchar)
+ ':'
+ Cast( Minute as varchar)
+ ':00' as datetime)
between
Dateadd(mi, -5,@picktime)
and
Dateadd(mi, 5, @picktime)
)
and
(CardID = 123456)