Reputation: 1070
Good day, I receive 2 nullable date parameters in my stored procedure. @StartDate and @EndDate. I need to execute my procedure as normal if these two parameters are NULL, else the @StartDate param needs to be >= my StartDateTime value AND @EndDate param needs to be <= my EndDateTime value. Below is a snippet of what I am trying to accomplish but are not sure of the syntax.
FROM DI_Intervention_Schedule S
WHERE
(
@ID IS NULL
OR S.[ID] = @ID
)
AND (
CASE @StartDate WHEN IS NOT NULL THEN
@StartDate >= S.[StartDateTime] AND @EndDate <= S.[EndDateTime]
END
)
Any help please?
Upvotes: 0
Views: 79
Reputation:
Try below code :
FROM DI_Intervention_Schedule S
WHERE
(S.[ID] = @ID OR @ID IS NULL)
AND (( S.[StartDateTime] >= @StartDate AND S.[EndDateTime] <= @EndDate) OR @StartDate IS NULL )
Upvotes: 1