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)
 Where clause first checking for is null

Author  Topic 

navs
Starting Member

13 Posts

Posted - 2007-07-23 : 03:05:45
How can I write a where clause that first checks if the value is passed to it or a null value. The following code doesnt work, but I need to know how to do something like this where the value is first checked for null and only if it is not null is that part of the code added to the where clause.



where
w.Login_ID = @from
and
x.Login_ID = @to
and if(@project_name is not null) {
y.Project_Name = @project_name and
}
and if(@project_name is not null) {
z.Group_Name = @group_name
}

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-07-23 : 03:09:19
[code]WHERE
w.Login_ID = @FROM
AND
x.Login_ID = @to
AND (
@project_name IS NULL
OR (
@project_name IS NOT NULL
AND y.Project_Name = @project_name
AND z.Group_Name = @group_name
)
)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -