developer77
Programmer
Hello everyone,
I have the following stored procedure that passes the start and end date range as parameters. I'm trying to pull all date ranges from the parameters based on the start date and end date of students enrolled. For example, I have the following start dates and end dates in the sql server database
StartDate EndDate
12/01/2006 12/10/2006
11/30/2006 12/10/2006
When I enter in a start date parameter of 12/01/2006 and an end date parameter of 12/07/2006, I would like the second record above to be pulled since the parameters do fall between 11/30/2006 and 12/10/2006. Currently it only pulls only the 1st record since it meets the date logic in the stored procedure. Please help me fix the date logic so that it works correctly. I appreciate any help I can get. Thanks.
CREATE PROCEDURE students_enrolled
(
@StartDateRange datetime = null,
@EndDateRange datetime = null
)
AS
--
BEGIN
SELECT s.FirstName,
s.LastName,
s.MiddleName,
e.student_id
FROM tbl_Student s
left join tbl_Enrollment e on e.id = s.EnrollID
WHERE s.active_flg = 'Y'
AND ((e.StartDate >= IsNull(@StartDateRange, e.StartDate) and e.StartDate <= IsNull(@EndDateRange, e.StartDate)) or (e.EndDate >= IsNull(@StartDateRange, e.EndDate) and e.EndDate <= IsNull(@EndDateRange, e.EndDate)))
END
RETURN
GO
I have the following stored procedure that passes the start and end date range as parameters. I'm trying to pull all date ranges from the parameters based on the start date and end date of students enrolled. For example, I have the following start dates and end dates in the sql server database
StartDate EndDate
12/01/2006 12/10/2006
11/30/2006 12/10/2006
When I enter in a start date parameter of 12/01/2006 and an end date parameter of 12/07/2006, I would like the second record above to be pulled since the parameters do fall between 11/30/2006 and 12/10/2006. Currently it only pulls only the 1st record since it meets the date logic in the stored procedure. Please help me fix the date logic so that it works correctly. I appreciate any help I can get. Thanks.
CREATE PROCEDURE students_enrolled
(
@StartDateRange datetime = null,
@EndDateRange datetime = null
)
AS
--
BEGIN
SELECT s.FirstName,
s.LastName,
s.MiddleName,
e.student_id
FROM tbl_Student s
left join tbl_Enrollment e on e.id = s.EnrollID
WHERE s.active_flg = 'Y'
AND ((e.StartDate >= IsNull(@StartDateRange, e.StartDate) and e.StartDate <= IsNull(@EndDateRange, e.StartDate)) or (e.EndDate >= IsNull(@StartDateRange, e.EndDate) and e.EndDate <= IsNull(@EndDateRange, e.EndDate)))
END
RETURN
GO