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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 When Parameter is be null?

Author  Topic 

Sep410
Posting Yak Master

117 Posts

Posted - 2008-08-11 : 17:37:15
Hi all,
I know it may seem funny for you but I am really confused right now.
I have a select like this:
SELECT CaseID, CaseIDText, IntakeWorkerID
FROM dbo.tbl_Case
WHERE (CaseID = @Param1) AND (CaseIDText = @Param2)

Sometimes user may not enter a value for @param2 how can I handle it in my code. I think I should use like statement but I don’t know how.
I should use this code in my vb.net application.
Please help me.



Sep

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-11 : 17:43:02
Use the COALESCE function to handle it in the WHERE clause.

WHERE CaseID = COALESCE(@Param1, CaseID) AND CaseIDText = COALESCE(@Param2, CaseIDText)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-12 : 03:04:44
or

WHERE (CaseID = @Param1 or @Param1 is null) AND (CaseIDText = @Param2 or @Param2 is null)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -