CREATE TABLE STUDY
(
[ID][INT],
STUDY_DATE VARCHAR(40),
START_TIME VARCHAR (40),
END_TIME VARCHAR (40)
)
INSERT INTO STUDY VALUES(1,'2013-12-26','11:30:00','11:31:00')
SELECT
STUDY_DATE,
START_TIME,
END_TIME
FROM
STUDY
WHERE
STUDY_DATE>='2013-12-25'
AND CONVERT(VARCHAR(8),START_TIME,108)>='18:41:34'
AND STUDY_DATE<='2013-12-26'
AND CONVERT(VARCHAR(8),END_TIME,108)<='12:41:34'
这里当start_time大于结束时间时,它给出零结果。 我该怎么办
答案 0 :(得分:1)
这不是处理日期和时间的推荐方法,但如果您无法更改数据类型:
SELECT
STUDY_DATE,
START_TIME,
END_TIME
FROM
STUDY
WHERE
CAST(STUDY_DATE + ' ' + START_TIME AS DATETIME) >= '2013-12-25 18:41:34' AND
CAST(STUDY_DATE + ' ' + END_TIME AS DATETIME) <= '2013-12-26 12:41:34'
--- --- ORIG
如果可以,您应该更加尊重适当的数据类型。您的设计还有其他选择 - 可能是:
CREATE TABLE STUDY
(
ID INT, -- Consider PRIMARY KEY IDENTITY(1,1)?
START_TIME DATETIME,
END_TIME DATETIME
);
这样你可以更恰当地使用日期/时间:
SELECT * FROM STUDY WHERE START_TIME >= '12/25/2013 18:41:34' AND END_TIME <= '12/26/2013 12:41:34'
答案 1 :(得分:0)
我猜你试图将开始时间与你的日期和第二次约会的结束时间分组。
如果是这样,你可以这样做:
SELECT STUDY_DATE,START_TIME,END_TIME
FROM STUDY
WHERE
STUDY_DATE BETWEEN '2013-12-25' AND '2013-12-26'
AND
(
(STUDY_DATE = '2013-12-25' AND CONVERT(VARCHAR(8),START_TIME,108)>='18:41:34')
OR
(STUDY_DATE = '2013-12-26' AND CONVERT(VARCHAR(8),END_TIME,108)<='12:41:34')
)
以下是fiddle