请问当前时间属于下面的哪个时间段?
能否用一个SQL语句来描述,假定定义starttime time(7),endtime time(7)用来保存时间
下面有三行两列表示时间段
时间段 startime 08:20-16:00 endtime
startime 16:01-20:00 endtime
startime 20:01-4:0 endtime
现在时间是21:00
按理应属于在20:01-4:0之间,但如果用一句SQL来查找呢
谢谢!
[解决办法]
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tba')BEGIN DROP TABLE tbaENDGOCREATE TABLE tba( StartTime VARCHAR(7), EndTime VARCHAR(7))GOINSERT INTO tbaSELECT '08:20','16:00' UNIONSELECT '16:01','20:00' UNIONSELECT '20:01','4:0'GODECLARE @Current VARCHAR(7)SET @Current = '21:00'SELECT * FROM tbaWHERE @Current BETWEEN StartTime AND CASE WHEN LEFT(EndTime,CHARINDEX(':',EndTime) - 1) < LEFT(StartTime,CHARINDEX(':',StartTime) - 1) THEN RTRIM(CAST(LEFT(EndTime,CHARINDEX(':',EndTime) - 1) AS INT) + 24) + RIGHT(EndTime,CHARINDEX(':',EndTime)) ELSE EndTime ENDStartTime EndTime20:01 4:0