1

我在 MS SQL 中有一个行表,其中包含一周的开始和结束日期、小时和时间。我需要一个 T-SQL 查询,它可以从 GETDATE 与这些行的星期几和时间匹配的表中提取行。具体来说,如果一行的日期/时间从一周中的一天开始并在第二天结束,我需要查询才能工作。

这是我正在使用的结构:

_start_day_of_week (int) = 5
_start_hour (int) = 15
_start_minute (int) = 30
_end_day_of_week (int) = 6
_end_hour (int) = 2
_end_minute (int) = 30
_title (string) = 'My Sample Row'
_id (int) = 1

给定当前的 DATETIME,我将如何检索这一行?

4

3 回答 3

0

I am still unclear about your requirements as they seem a bit impracticle. But here is my go.

DECLARE @A AS TABLE(
_start_day_of_week int,
_start_hour int,
_start_minute int,
_end_day_of_week int,
_end_hour int,
_end_minute int,
_title varchar(10),
_id int 
)
--Above is temp table for example
INSERT @A
SELECT 5,15,30,6,2,30,'SAMPLE 1', 1 UNION ALL
SELECT 6,15,30,6,17,30,'SAMPLE 2', 2

DECLARE @month int = datepart(month,getdate())
DECLARE @currentDay int = datepart(dw,getdate())
DECLARE @currentHour int = datepart(hour, getdate())
DECLARE @currentMinute int = datepart(minute, getdate())



SELECT * FROM @A --Don't use select *. This is just an example.
WHERE --Where GETDATE matches
_start_day_of_week = @currentDay --The day of week
AND
_start_hour = @currentHour --and time of those rows
AND
_start_minute = @currentMinute
--I have not done any matching against the _end columns as
--your criteria are vague. Should it exclude all days except
--the current day and the currentday+1?
-- But you would use a similar method.
于 2011-02-05T15:20:29.090 回答
0

你可以尝试类似的东西:

select * from [yourtable]
where yourdatefield between getDate() and (getDate()+1 )
于 2011-02-05T03:59:24.117 回答
0

您应该能够使用datepart函数来使用getdate函数:

http://msdn.microsoft.com/en-us/library/aa258265%28v=sql.80%29.aspx

于 2011-02-05T04:00:39.377 回答