SQL - Dynamically build where clause

I wrote inline SQL in a C# .NET application but it is timing out so I am creating a SPROC in sql server.  

I need to pass in a parameter for the purpose of dynamically determining the where clause.  Any idea how to do this?

As an example - lets say I am passing in CheckDate as BIT, dtStart and dtEnd.

If CheckDate = 0 then

    Where MyDate > '1/1/13'

if CheckDate = 1 and the dtStart = '5/1/13' and dtEnd IS NULL

    Where MyDate >= '5/1/13'

if CheckDate = 1 and the dtStart = '5/1/13' and dtEnd = '5/31/13'

    Where MyDate >= '5/1/13' and MyDate <= '6/1/13'

Thanks
LVL 2
CipherISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
WHERE CASE ... will work if you're filtering on a single value with a single expression, but not with multiple, and afaik even using a nested CASE will get somewhat clunky.

you can try a big 'ol OR block with all of these choices

WHERE
    (CheckDate = 0 AND MyDate > '1/1/13') OR
    (CheckDate = 1 AND dtstart = '5/1/13' and dtEnd IS NULL AND MyDate >= '5/1/13') OR
    (CheckDate = 1 AND dtstart = '5/1/13' and dtEnd = '5/31/13' AND MyDate >= '5/1/13' AND MyDate <= '6/1/13')
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CipherISAuthor Commented:
Awesome!  Thanks
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.