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)
 Help needed

Author  Topic 

kwikwisi
Constraint Violating Yak Guru

283 Posts

Posted - 2010-02-23 : 21:08:44

select ----
from ---
where condition1 , condtion2,...

I want to select based on @param. Current statement is as above.

Select all records
Select the records where tbl1.col1 is not null if @param is not null

Thanks.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-02-23 : 21:14:56
[code]
select *
from sometable
where (
@param is null
or somecol = @param
)
[/code]


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

Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

283 Posts

Posted - 2010-02-23 : 21:23:17
I just want to use @param as flag. If it is not null [true] i will select records where col1 is not null. Otherwise, I will select records and not checking col1 .

quote:
Originally posted by khtan


select *
from sometable
where (
@param is null
or somecol = @param
)



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



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-02-23 : 21:32:21
[code]
select *
from sometable
where (
@param is null
or somecol = @param
(@param is not null and col1 is not null)
)
[/code]


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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-24 : 10:06:50
quote:
Originally posted by khtan


select *
from sometable
where (
@param is null
or somecol = @param
(@param is not null and col1 is not null)
)



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




can be reduced as

select *
from sometable
where (
@param is null
or col1 is not null
)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -