conditional where clause in sql 2012 - sql

I have a query as below in my Stored Procedure:
I set the #SUBCONDITION FROM MY CODE BEHIND.
declare #SUBCONDITION VARCHAR(100)= NULL
if(#SUBCONDITION='DEPT')
BEGIN
Select * from table1 where IDDEPT=1
END
ELSE IF (#SUBCONTION='GRADE')
BEGIN
SELECT * FROM TABLE1 WHERE IDGRADE=1
END
ELSE IF(#SUBCONDITION='SECTION')
BEGIN
SELECT * FROM TABLE1 WHERE IDSECTION=1
END
Everything works just fine.
My question is whether I can do this in just one query??? Like using if or case or something in the where clause?

Yes, you can do this in one query. It would look like this:
Select *
from table1
where (#SUBCONDITION = 'DEPT' and IDDEPT = 1) or
(#SUBCONTION = 'GRADE') and IDGRADE = 1) or
(#SUBCONDITION = 'SECTION' and IDSECTION = 1)

Try this:
SELECT *
FROM table1
WHERE 1 = (CASE #SUBCONDITION
WHEN 'DEPT' THEN IDDEPT
WHEN 'GRADE' THEN IDGRADE
WHEN 'SECTION' THEN IDSECTION
ELSE 0
END);

Try this:
Select * from table1
where #SUBCONDITION = 'DEPT' AND IDDEPT=1 OR
#SUBCONDITION = 'GRADE' AND IDGRADE=1
#SUBCONDITION = 'SECTION' AND IDSECTION=1

Related

How to use case statement inside an SQL select Query

I need to get the output of a selected query depending on certain conditions
Means if(id=uid)
then I need the below query
select * from table1 where id=5;
else
I need the below one
select * from table1 where id=10
I know i can use if condition for this. But my query is very long one so when I use if else then it would look like
if(#id=#uid)
begin
select * from table1 where id=5;// query 1
end
else
select * from table1 where id=10;//query 2
but here I need to replace the entire query once again for a single check. I hope I can do something like this:
declare #id int=4;
declare #uid=10;
select * from table1 where
case
when #id=#uid
then
id=5
else
id=10;
end
Updation
I need one more condition too
in this case id=5 and uid=10
then if(id=uid)
then
select * from table1 where id=5
and
if(id!=uid)
then
select * from table1
something like this
You can use the case expression to return the value id should be equal to:
SELECT *
FROM table1
WHERE id = CASE WHEN #id = #uid THEN 5 ELSE 10 END;
EDIT:
The updated requirement in the question is to return all rows when #id != #uid. This can be done by comparing id to id:
SELECT *
FROM table1
WHERE id = CASE WHEN #id = #uid THEN 5 ELSE id END;
Alternatively, with this updated requirement, a simple or expression might be simpler to use:
SELECT *
FROM table1
WHERE #id = #uid OR id = 5;
SELECT * FROM table1
WHERE (id=5 AND #id=#uid) OR (id=10 AND #id<>#uid)
SELECT
*
FROM
table1
WHERE
(
#id = #uid
AND
id =5
)
OR
(
not #id = #uid
AND
id=10
)

Select Statement based on user defined variable

I am trying to do a simple select statement based on the input of the user (SSRS). Help!
select * from Table1
WHERE
Case when #x = 'Yes' then (select * from Table1 where [Column1] < 0) end;
case when #x = 'No' then (select * from Table1 where [Column1] > 0) end;
Thank you in advance
KJ
It should be as simple as
select * from Table1
WHERE
(#x = 'Yes' AND [Column1] < 0)
OR
(#x = 'No' AND [Column1] > 0);
By the way, SELECT * is really bad coding, you really should specify each column you are returning.

Can i have multiple select but only return one result set

If I have multiple selects like so:
select * from A where A.name = 'linköping'
IF ##ROWCOUNT = 0
begin
select * from A where A.amount = 45
end
...I get 1 result set if the first select returns stuff. But if it runs the second, I get two result sets; the first with no rows and the second with some rows.
Is there a way to only return the second result set if the second select is run ?
I write code like this because of Andrey Gordeev's answer to this post: Can you have if-then-else logic in SQL?
(MSSQL 2000)
Thanks!
You will need to prevent the first select by checking if you will get any results back before running the select.
For example:
IF EXISTS (SELECT 1 FROM A WHERE A.name = 'linköping')
BEGIN
SELECT * FROM A WHERE A.name = 'linköping'
END
ELSE
BEGIN
SELECT * FROM A WHERE A.amount = 45
END
No it is not possible The returning results are depending on number of select statements.
If you want to do the single select follow #RB or #Jwalin Shah solution.
This will work with IF-ELSE Logic. [Tested]
DECLARE #count as int
set #count = (SELECT count(1) from A where A.name = 'linköping')
if (#count = 0)
BEGIN
select * from A where A.amount = 45
END
ELSE
BEGIN
select * from A where A.name = 'linköping'
END
Try this
IF ##ROWCOUNT = 0
BEGIN
select * from A where A.amount = 45
END
ELSE
BEGIN
select * from A where A.name = 'linköping'
END

TSQL where case

I need the correct tsql syntax for this problem :
Select * from table where var_A='10'
select * from table where var_B='10'
When to use var_A or var_B depends on the value of var_C
So when var_c='something' use var_A='10' else var_B='10'
select *
from [table]
where case when var_c = 'something' then var_a else var_b end = '10'
SQL Fiddle with demo.
Ian probably has the best answer available but I thought I would supply another answer for a different (although less flexible) method in case it helps:
IF var_c = 'something' BEGIN
SELECT * FROM [table] WHERE var_a = '10'
END
ELSE BEGIN
SELECT * FROM [table] WHERE var_b = '10'
END
This how you use case statement within where clause.
select * from table
where ( case when var_c = 'something'
then var_a
else
var_b
end
)

How do I use an if statement inside the where clause in this sql statement?

select *
from mytable
where (if #key=0
pkey>=0
else
pkey = #key)
#key is the value passed in to the stored procedure and pkey is a column in mytable.
How about this:
select *
from mytable
where ((#key=0 AND pkey>=0) OR (#key<>0 AND pkey = #key))
You use CASE (bit like the way you are trying with if) as below. (DEMO)
select *
from mytable
where pkey = case when #key <> 0 then #key
else Abs(pkey) end
This will work for you . by using case statement you can apply dynamic filter ..
select *
from mytable
where 1 = case
when #key=0 then case when pkey>=0 then 1 else 0 end
else case when pkey = #key then 1 else 0 end
end