I have this stored procedure:
ALTER PROCEDURE [dbo].[GetCalendarEvents]
(@StartDate datetime,
@EndDate datetime,
@Location varchar(250) = null)
AS
BEGIN
SELECT *
FROM Events
WHERE EventDate >= @StartDate
AND EventDate <= @EndDate
AND (Location IS NULL OR Location = @Location)
END
Now, I have the location parameter, what I want to do is if the parameter is not null then include the parameter in where clause. If the parameter is null I want to completely ignore that where parameter and only get the result by start and end date.
Because when I'm doing this for example:
EXEC GetCalendarEvents '02/02/2014', '10/10/2015', null
I'm not getting any results because there are other locations which are not null and since the location parameter is null, I want to get the results from all the locations.
Any idea how can I fix this?