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 2012 Forums
 Transact-SQL (2012)
 Conditional where

Author  Topic 

ArnoldG
Starting Member

36 Posts

Posted - 2014-01-25 : 09:16:34
Hi,

I am running a SQL query in Excel with an external parameter PARAM from a cell with values 'Yes' or 'No':


SELECT A,B,C
FROM Table


The C column either contains NULL or 1

How do I filter in the WHERE clause so that I get:
ALL results if PARAM is 'No'
C= 1 if the PARAM is 'Yes'

I tried several things with SELECT CASE, but up until now without any results.
Can anyone help me out here ?

Thx,
Arnold

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-01-27 : 01:39:49
Try this

SELECT A,B,C
FROM Table
WHERE (PARAM ='No') or
(C= 1 AND PARAM ='Yes')

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-27 : 07:38:08
quote:
Originally posted by madhivanan

Try this

SELECT A,B,C
FROM Table
WHERE (PARAM ='No') or
(C= 1 AND PARAM ='Yes')

Madhivanan

Failing to plan is Planning to fail


simply this would do it right


SELECT A,B,C
FROM Table
WHERE (@PARAM = 'No'
OR C = 1)

Assuming PARAM allows only values as Yes and No





------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-01-27 : 08:10:58
quote:
Originally posted by visakh16

quote:
Originally posted by madhivanan

Try this

SELECT A,B,C
FROM Table
WHERE (PARAM ='No') or
(C= 1 AND PARAM ='Yes')

Madhivanan

Failing to plan is Planning to fail


simply this would do it right


SELECT A,B,C
FROM Table
WHERE (@PARAM = 'No'
OR C = 1)

Assuming PARAM allows only values as Yes and No





------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs



Yes it is

Madhivanan

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

ArnoldG
Starting Member

36 Posts

Posted - 2014-01-27 : 09:01:55
visakh16 and madhivanan,

Thanks for your help.
This works perfectly ...and so simple !

Thanks again.
Arnold
Go to Top of Page
   

- Advertisement -