Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
SQL
create procedure sp_get_report(@datefrom date=null,@dateto date=null,@customerfrom nvarchar(40)=null,@customerto nvarchar(40)=null,@salesrepfrom nvarchar(40)=null,@salesrepto nvarchar(40)=null)
as

begin

select total from employee where alldate between @datefrom and @dateto
and customer between @customerfrom and @customerto 
and sales between @salesrepfrom and @salesrepto

end



exec sp_get_report '2014-10-01','2014-10-03' -- if i did not pass @customerfrom,@customerto,@salesrepfrom,@salesrepto, i get null. becuase i used optional parameter for condition.

so if any set of between and condition is null, that particular betwenn and should not use.

we need use between and condition whatever is not null only. i guess it could be done using case any other way. but i am in learing process only.

if anyone knows, help me.
Posted
Updated 3-Oct-14 5:56am
v2

try this

SELECT
*
FROM employee e
WHERE (e.FromDate >= @datefrom
OR @datefrom IS NULL)
AND (e.ToDate < @dateto
OR @dateto IS NULL)
 
Share this answer
 
v2
How about something along the lines of : ( alldate IS NULL OR alldate between @datefrom and @dateto )
 
Share this answer
 
i would just add a conditional statement to set dates to an absurd range when they are missing. i hate using greater than, less than in dates (for some reason)

ie...
SQL
create procedure sp_get_report(@datefrom date=null,@dateto date=null,@customerfrom nvarchar(40)=null,@customerto nvarchar(40)=null,@salesrepfrom nvarchar(40)=null,@salesrepto nvarchar(40)=null)
as

begin
if @datefrom is null set @datefrom = '1/1/1900'
if @dateto is null set @dateto = '1/1/2050'
etc...
select total from employee where alldate between @datefrom and @dateto
and customer between @customerfrom and @customerto
and sales between @salesrepfrom and @salesrepto

end
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900