tags:

views:

228

answers:

6

Hi, I have one table as Follows

ID        bigint
TaskName  nvarchar(50)
StartDate datetime      -- Start date of task
EndDate   datetime      -- End Date Of Task

I am trying to Write One Procedure Which Takes Two inputs @FromDate and @EndDate as datetime, To get list of TaskNames Which are in process between input parameters.

Can any body help me to write the Where condition for it

Thanks And Regards, Rohan

A: 

Something like the following should work for you:

select * from myTable
where StartDate > @FromDate
and EndDate < @EndDate

Or something like

select * from myTable
where StartDate between @FromDate and @EndDate

The first one gets all tasks that are in between your from and end dates. The second one gets all tasks that were started (regardless of end time) between your from and end dates.

Chris Lively
A: 

you don't specify if you want to consider time or include endpoints, but I'm not going to write out each possible combination... this considers time and allows for inclusion of end points:

SELECT
    *
    FROM YourTable
    WHERE StartDate>=@StartDate AND EndDate<=@EndDate
KM
A: 
WHERE (StartDate < @Fromdate ) AND ( EndDate > @EndDate )

will give the tasks that started earlier than @FromDate but not finished before @EndDate.

Alex_L
+4  A: 
WHERE StartDate <= @EndDate AND EndDate >= @FromDate

Should do the trick

Edit: Sample code I used to test various scenarios:

DECLARE @Data TABLE (StartDate DATETIME, EndDate DATETIME, ID INTEGER)
INSERT @Data 
SELECT '2009-05-10','2009-05-20', 1
UNION
SELECT '2009-05-15','2009-05-20', 2
UNION 
SELECT '2009-05-01','2009-06-01', 3

DECLARE @FromDate DATETIME
DECLARE @EndDate DATETIME
SET @FromDate = '2009-05-11'
SET @EndDate = '2009-05-18'

SELECT * FROM @Data
WHERE StartDate <= @EndDate AND EndDate >= @FromDate
AdaTheDev
this will return tasks that start before the range and end after the range, not ones within the range
KM
@KM - no that's not true. Did you try it? I will edit my post with sample code I used to test various scenarios
AdaTheDev
when I run your code, I get the row for "2009-05-01" - "2009-06-01" and to me that is not within your given date range of "2009-05-11" - "2009-05-18"
KM
I had to look at this one again. @AdaTheDev is right. This problem has many subtle ways to fail so props(upvote) from me.
Kelly French
@KM - that's correct though isn't it? That row *is* in process between 2009-05-11 and 2009-05-18, it's just not *entirely* within the supplied date range but it was by definition "in process" between those dates.
AdaTheDev
@AdaTheDev, I guess it depends on how you read the question. It isn't entirely clear to me if the OP wants rows that are entirely between the given range or partially between the given range.
KM
This will only find tasks that completely fall within the specified dates, not ones which partially overlap. I have included the where clause to detect partial overlaps in my answer.
Bork Blatt
@Bork Blatt - but it doesn't. Please can you given an example @FromDate and @EndDate to slot into my example code, that causes a problem? I've tried a load of various scenarios and can't find what you mean.
AdaTheDev
@Ada - OK - I have incorporated your WHERE clause, and now it will catch not only partial overlaps but cases where the task is started and completed within the interval specified.
Bork Blatt
A: 

You could do this.

Create procedure sp_name
(@fromdate as datetime,
@Enddate as datetime
) 
as

select * from tablename
where StartDate between @FromDate and @EndDate
Eric
A: 

UPDATE: After a good discussion with Ada, I see how his query is the optimal solution. While my answer below will work, it's redundant. I'll leave this answer here for history's sake. +1 to Ada's answer.

As I understand the question, he wants to retrieve any tasks that were being worked on at any point between the dates specified. This implies partially overlapping tasks as well as tasks which started and finished during the time interval.

Two time slots overlap if:

  • The start time on task 1 falls anywhere between the start and end times of task 2 OR
  • The end time on task 1 falls anywhere between the start and end times of task 2 OR
  • Start and end times of

I have used this method successfully in a reservations system to prevent any overlap in bookings.

So far in the answers I haven't seen a query that checks both of these conditions. Here's my attempt:

WHERE ( (StartDate >= @FromDate) AND (StartDate <= @EndDate) )
OR ( (EndDate >= @FromDate) AND (EndDate <= @EndDate) )
OR ( (StartDate <= @EndDate) AND (EndDate >= @StartDate) )
Bork Blatt
This doesn't work. If you slot that WHERE into the example code in my edit with @FromDate=2009-05-16 and @EndDate=2009-05-19, it doesn't return any results.
AdaTheDev
OK - with the correction, it should now work when the entire task fits into the interval, as well as if any overlap is occurring.
Bork Blatt
Now you're at the point where the first 2 WHERE conditions are redundant, hence you only need the final condition to meet all scenarios...as per my original answer
AdaTheDev
OK I see it. Hey - thanks for the back and forth - I've learned something. I'll edit my question to refer them to yours, and just let my overcomplicated answer linger for historic purposes.
Bork Blatt
No worries - it was the kind of process I followed when I had a need for this kind of query in a project I once worked on, chasing my tail until I settled to that end result!
AdaTheDev