Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
lar
Starting Member
1 Post |
Posted - 2011-01-07 : 18:15:59
|
| Hello,I'm using asp.net and passing a parameter to a stored procedure which is true or false. My problem is that I'm not sure how to create a condition that create the correct statement. Example:@dateContacted bit,WHERE (dbo.Insurance_ContactForm.assignedagent IS NOT NULL) AND dbo.Insurance_ContactForm.SubmitDate BETWEEN @StartDate AND @EndDate AND dbo.Insurance_ContactForm.region IN (SELECT IntValue From dbo.SpitToInt(@regionid)) AND TypeOfInsurance LIKE '%' + @product + '%' AND closed = @Closed AND Sold = @Sold AND dateContacted >>>>Hope it some way to accomplish thisif @dateContacted is true then dateContacted IS NOT NULLif it is false then dateContacted IS NULLif it is not selected at all NEITHERThanks Lar |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2011-01-07 : 18:38:22
|
If your intent is to conditionally apply the dateContacted piece of the where clause, you will have to do something like this:The parameter for bit type would have to be one or the other. IF @dateContacted = true BEGINSelect stuffFrom somewhereWHERE (dbo.Insurance_ContactForm.assignedagent IS NOT NULL) AND dbo.Insurance_ContactForm.SubmitDate BETWEEN @StartDate AND @EndDate AND dbo.Insurance_ContactForm.region IN(SELECT IntValue From dbo.SpitToInt(@regionid)) AND TypeOfInsurance LIKE '%' + @product + '%' AND closed = @Closed AND Sold = @Sold AND dateContacted is not nullENDIF @dateContacted = false BEGINSelect stuffFrom somewhereWHERE (dbo.Insurance_ContactForm.assignedagent IS NOT NULL) AND dbo.Insurance_ContactForm.SubmitDate BETWEEN @StartDate AND @EndDate AND dbo.Insurance_ContactForm.region IN(SELECT IntValue From dbo.SpitToInt(@regionid)) AND TypeOfInsurance LIKE '%' + @product + '%' AND closed = @Closed AND Sold = @Sold AND dateContacted is nullENDIF @dateContacted is nullBEGINSelect stuffFrom somewhereWHERE (dbo.Insurance_ContactForm.assignedagent IS NOT NULL) AND dbo.Insurance_ContactForm.SubmitDate BETWEEN @StartDate AND @EndDate AND dbo.Insurance_ContactForm.region IN(SELECT IntValue From dbo.SpitToInt(@regionid)) AND TypeOfInsurance LIKE '%' + @product + '%' AND closed = @Closed AND Sold = @SoldEND The other option would be dynamic SQL. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-08 : 01:06:31
|
you can reduce it to:-Select stuffFrom somewhereWHERE (dbo.Insurance_ContactForm.assignedagent IS NOT NULL) AND dbo.Insurance_ContactForm.SubmitDate BETWEEN @StartDate AND @EndDate AND dbo.Insurance_ContactForm.region IN(SELECT IntValue From dbo.SpitToInt(@regionid)) AND TypeOfInsurance LIKE '%' + @product + '%' AND closed = @Closed AND Sold = @Sold AND ((dateContacted is not null AND @dateContacted = 'true') OR (dateContacted is null AND @dateContacted = 'false') OR @dateContacted is null) change 'true','false' to 1,0 if field is of bit type------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|