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 2008 Forums
 Transact-SQL (2008)
 Query Help

Author  Topic 

xpandre
Posting Yak Master

212 Posts

Posted - 2013-12-10 : 12:58:13
I need a query like :

declare @afilter varchar(100) = 'test'
select top 10 * from table1
where
case when @afilter = 'test' then col1 = 'test'
else col1!='test' end

Its like : if @afilter is 'test' then return everything from the table which has col1 = 'test'
else return records from table1 where col1!='test'

Any help appreciated.

Thanks
Sam

xpandre
Posting Yak Master

212 Posts

Posted - 2013-12-10 : 13:02:34
I did something like below..but I think its much more easier than this:(

DECLARE @afilter VARCHAR (50)
set @afilter='test'

select top 10 * from
(
SELECT * , case when L1.col1 = 'test' then 1 else 0 end caseclient
From table1 L1
)a
where case
when @afilter='test' then 1 else 0 end =caseclient
Go to Top of Page
   

- Advertisement -