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

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

Related

input parameter used in case statement in where clause

I have the following simplified stored procedure where based on on the input parameter, I need to then do a case in the where clause. It will not execute as it says: Incorrect syntax near '='
PROCEDURE [dbo].[DataInfo]
#Allowactive BIT = 1
AS
BEGIN
Select * from tbl1 1
where (CASE #Allowactive
WHEN 0 then (t.Isactive = 1) END
AND isSubmitted = 1
END
your where clause will be like below
where
CASE #Allowactive
WHEN 0 then t.Isactive END =1
AND isSubmitted = 1
You shouldn't use parameters in a query like this, as it messes up the query plan. When the right plan to use changes depending on the parameter, you need separate queries, or to force SQL to always recompile.
So do this instead:
create or alter procedure [dbo].[DataInfo] #Allowactive bit = 1
as
begin
if #Allowactive = 0
begin
Select * from tbl1 1
where Isactive = 1
AND isSubmitted = 1
end
else
begin
select * from tbl1 1
where isSubmitted = 1
end
end
Instead run separate queries.
Try to run the following and see the results:
SELECT *
FROM Tbl1 AS T
WHERE CASE #Allowactive
WHEN 0 THEN 1 ELSE #Allowactive END = T.Isactive
AND
isSubmitted = 1;
If you have 2012+ version then you could also do:
SELECT *
FROM Tbl1 AS T
WHERE IIF(#Allowactive = 0, 1, #Allowactive) = T.Isactive;
It seems Zaynul Abadin Tuhin directly answers your question.
But, I believe a case statement complicates what you want to achieve.
I think a query like this satisfies your desired outcome:
PROCEDURE [dbo].[DataInfo]
#Allowactive BIT = 1
AS
BEGIN
SELECT * FROM tbl1 t
WHERE (#Allowactive = 1 OR (#Allowactive = 0 AND t.Isactive = 1))
AND t.isSubmitted = 1
END

Range of values as ELSE clause in a CASE statement

I have a stored proc which is supposed to filter results based on an integer which can be NULL and I need to be able to choose results that either match the integer or retrieve all results but I can't figure out the syntax for the "All" results part. Query is basically this:
Select * from dbo.TABLE
WHERE [IDField] =
CASE
WHEN ISNULL(#ID, 0) > 0 THEN #ID
ELSE ?????
END
I want to use an IN range to include all the values because setting it to 0 or Null will return no results, but this doesn't work:
Select * from dbo.TABLE
WHERE [IDField] =
CASE
WHEN ISNULL(#ID, 0) > 0 THEN #ID
ELSE 1 OR 2 OR 3
END
or this:
Select * from dbo.TABLE
WHERE [IDField] IN (
CASE
WHEN ISNULL(#mID, 0) > 0 THEN #mID
ELSE 1,2,3
END
)
Phrase this without the case:
SELECT *
FROM dbo.TABLE
WHERE #id is null OR IDField = #id;
Then you can expand this to:
SELECT *
FROM dbo.TABLE
WHERE (COALESCE(#ID, 0) > 0 AND IDField = #id) OR
IDField IN (1, 2, 3)

Choose multiple cases condition

I want to get multipule choises after then in case statment as
#value
select * from [dbo].[Currency_Tbl]
WHERE [Currency_Active_YN]=
CASE WHEN #value = 1 THEN
( 1 or 0)
ELSE
#Value = 0 then 0
END
it didn't accept the first line in col1 but accept the col2
how can I select multiple numbers after THEN?
You don't use case in where clauses. Use boolean logic
select * from [dbo].[Currency_Tbl]
WHERE (#value = 1 and [Currency_Active_YN] in (0,1))
OR (#value = 0 and [Currency_Active_YN] = 0)
You dont need a case to do what you're trying to do. Assuming Currency_Active_YN is a not null bit field the following logic should suffice.
select * from [dbo].[Currency_Tbl]
WHERE (#value=1 OR [Currency_Active_YN]=#Value)

IF ELSE in where block of sql query

I have the below stored procedure
CREATE PROCEDURE sp1
(
#param1 bit,
#parma2 bit
)
AS
BEGIN
SELECT * from Table1 where
if #param1 = 1 then column1 is null
else if #parma2 = 1 then column1 is not null
END
Obviously the If else part in the where clause is not correct . Could anyone suggest how to tackle it ?
Something like this:
SELECT * from Table1 where
case
when #param1 = 1 then [column 1] is null
when #param2 = 1 then [column 1] is not null
end
But you don't need two parameters for this procedure, only one should suffice.
Try the following:
SELECT
Case WHEN #param1 = 1 THEN Null
Case WHEN #param2 = 1 THEN column1 Else '' End as column1 ,
* FROM Table1
The above statement will set column1 to null if param1 = 1, it will get the value of column1 if param2 = 1 otherwise it will set it to empty string.
Seems to me like the simplest option would be to use AND and OR:
SELECT *
FROM Table1
WHERE (#param1 = 1 AND column1 is null)
OR (#parma2 = 1 AND column1 is not null)
Use this:
CASE WHEN condition THEN value ELSE value END

conditional where clause in sql 2012

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