I have a stored procedure which returns data used in a report. There are numerous paramters the user can specify, two of which are a start and end date. I can use a WHERE create_date BETWEEN @arg_start AND @arg_end statement to filter on the dates.
What I dont know how to do is to handle the situation where the user doesnt supply any dates. CASE doesnt support anything like WHERE create_date = CASE @arg_start WHEN NULL THEN create_date ELSE BETWEEN @arg_start AND @arg_end.
I’ve done a lot of research on google, msdn, and here and I haven’t find out to handle conditional null datetime processing. Although I know its bad form, I can pass programatically pass in a magic date, such as 1/1/1900, to test instead of a null, but that doesn’t really help in conditional date processing.
I would make the default values for the stored procedure parameters the “magic dates”:
In your application, if the user has not specified a date, don’t pass that parameter to the stored proc.
This would keep your application code clean, and give you the data you need.