January 20, 2014 at 11:56 pm
i have a requirement to get the appropriate RunID based on the Starttime.
I'm not able to get results if Starttime is beween 00:00 and 05:00 for ABC Dataset. Please help.declare @Schedule Table(Dataset sysname,RunID SMALLINT,StartMinTime TIME)
INSERT INTO @Schedule
select 'ABC',1,'22:01' union ALL
select 'ABC',2,'05:01' union ALL
select 'ABC',3,'14:01' union ALL
select 'XYZ',1,'03:01' union ALL
select 'XYZ',2,'12:01' union ALL
select 'XYZ',3,'20:01'
Declare @DatasetName sysname = 'XYZ'
Declare @StartTime datetime = '2014-01-19 01:21:39.000'
Select RunID,@StartTime, CAST(CAST(@StartTime AS DATE) AS DATETIME) + CAST(StartMinTime AS TIME)
from @Schedule where Dataset = @DatasetName
and DATEPART(hour,cast(@StartTime as TIME)) >= DATEPART(hour, StartMinTime) and DATEPART(hour,cast(@StartTime as TIME)) <= (DATEPART(hour, StartMinTime) + 8)
January 21, 2014 at 2:46 am
Move your search predicates from the WHERE clause to the output. This a) gives you some output and b) shows you why it doesn't work:
--I'm not able to get results if Starttime is beween 00:00 and 05:00 for ABC Dataset. Please help.
DECLARE @Schedule Table(Dataset sysname, RunID SMALLINT, StartMinTime TIME)
INSERT INTO @Schedule
select 'ABC',1,'22:01' union ALL
select 'ABC',2,'05:01' union ALL
select 'ABC',3,'14:01' union ALL
select 'XYZ',1,'03:01' union ALL
select 'XYZ',2,'12:01' union ALL
select 'XYZ',3,'20:01'
SELECT * FROM @Schedule
DECLARE @DatasetName sysname = 'XYZ'
DECLARE @StartTime datetime = '2014-01-19 01:21:39.000'
SELECT
RunID,
[@StartTime]= @StartTime,
-- CAST(CAST(@StartTime AS DATE) AS DATETIME) + CAST(StartMinTime AS TIME), -- fails, cannot add TIME to DATETIME
CAST(CAST(@StartTime AS DATE) AS DATETIME),
CAST(StartMinTime AS TIME), -- StartMinTime IS time
[From]= DATEPART(hour, StartMinTime),
[SearchValue]= DATEPART(hour,cast(@StartTime as TIME)),
[To]= (DATEPART(hour, StartMinTime) + 8)
FROM @Schedule
WHERE Dataset = @DatasetName
--and DATEPART(hour,cast(@StartTime as TIME)) >= DATEPART(hour, StartMinTime)
--and DATEPART(hour,cast(@StartTime as TIME)) <= (DATEPART(hour, StartMinTime) + 8)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply