1

I have a table of rows in MS SQL that contain a start and end day of the week, hour, and time. I need a T-SQL query that can pull rows from that table where GETDATE matches the day of week and time of those rows. Specifically, I need the query to work if a row has a day/time that starts on one day of the week and ends on the next day.

Here's the structure I'm working with:

_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

How would I retrieve this row given the current DATETIME?

2
  • Could you post table DDL, some sample data, and the result you expect. It would help us understand your requirements better knowing what sort of data types you are talking about. Feb 5, 2011 at 4:08
  • Sure, I've added some more detail about the table structure. @Robert Kaucher @Victor
    – Corgalore
    Feb 5, 2011 at 4:20

3 Answers 3

0

you could try something like:

select * from [yourtable]
where yourdatefield between getDate() and (getDate()+1 )
0

You should be able to use datepart function to go with getdate function:

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

2
  • Assuming he has datetime datatypes; which it is not clear is the case. The OP keeps using the term day rather than date and that makes me wonder how the table is structured. Feb 5, 2011 at 4:11
  • I'm sorry, limic. I catch your drift now. Feb 5, 2011 at 15:19
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.
1
  • Well, my code is still not working perfectly. The challenge is to select a number of records that contain the start and end day/hour/minute of a food/drink special. I need to identify which records represent specials that are active right now, as in the moment the user views the page. It becomes difficult because sometimes the specials start on one day and end on another. I would like to do this in T-SQL if possible. @RobertKaucher
    – Corgalore
    May 6, 2011 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.