case statement in where clause depending on variable value - sql

Depending on variable value my Where condition should change
If #CustID <> 0 Then
Where SomeColumn = #BillID
Else
Where SomeColumn In (#BillID,0)
My Query:
Select *
From SomeTable
Where SomeColumn = (Case When #CustID <> 0 Then #BillID Else /* What to Write Here */ End)

Here is the query you're looking for:
SELECT *
FROM SomeTable ST
WHERE (#CustID <> 0 AND ST.SomeColumn = #BillID)
OR (#CustID = 0 AND ST.SomeColumn IN (#BillID, 0))
Hope this will help.

Related

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)

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

SQL Nested CASE - shortcut logic?

I am trying to get rid of all of my dynamic SQL code. This stored procedure seems to require some nested CASE statements withing the WHERE clause. This is working as expected. However, I am worried about the performance. Will the nested ( GuarantorNumber not in ( select... ) ) statements be executed if the parent CASE statements are not true?
WHERE 1 = CASE WHEN ( #method='ADDED_RECORDS' ) THEN
CASE WHEN
( pt.[GuarantorNumber] not in (select cpg.GuarantorNumber from [CorepointProtectedGuarantor] cpg) ) THEN
1
ELSE 0 END -- NEW_RECORDS
WHEN ( #method='DELETED_RECORDS' ) THEN
CASE WHEN
( pt.[GuarantorNumber] not in (select mpg.GuarantorNumber from [MeditechProtectedGuarantor] mpg) ) THEN
1
ELSE 0 END -- DELETED_RECORDS
WHEN ( #method='UPDATED_RECORDS' ) THEN
CASE WHEN
( pt.[GuarantorNumber] in
(SELECT mpg.GuarantorNumber FROM
[MeditechProtectedGuarantor] mpg,
[CorepointProtectedGuarantor] cpg
WHERE
mpg.GuarantorNumber = cpg.GuarantorNumber and
(mpg.[LastName] <> cpg.[LastName] OR
mpg.[FirstName] <> cpg.[FirstName] OR
mpg.[MiddleName] <> cpg.[MiddleName] OR
mpg.[Address] <> cpg.[Address] OR
mpg.[AddressLine2] <> cpg.[AddressLine2] OR
mpg.[City] <> cpg.[City] OR
mpg.[State] <> cpg.[State] OR
mpg.[ZIP] <> cpg.[ZIP] OR
mpg.[Phone] <> cpg.[Phone])
) -- end of SELECT clause
) -- end of WHEN clause
THEN
1
ELSE 0 END -- UPDATED_RECORDS
Searched CASE expression:
Returns the result_expression of the first input_expression = when_expression that evaluates to TRUE.
In your case if #method='ADDED_RECORDS' and #method='DELETED_RECORDS' are false then the next statement will be executed WHEN(#method='UPDATED_RECORDS') In SQLServer2008 to analysе this behavior symply by pressing Cntl+L.
Declare #query nvarchar(max)
Declare #selectoneCount int
Declare #Temp int
SET #Temp =1
Declare #val nvarchar(max)
SET #val =''
select
#query = case #Temp
when 1
then
case #val
when 'NULL'
then
('Select Diameter from BallsProbes where Remark =2')
else
('Select Diameter from BallsProbes where Remark =3')
END
when 2
then ('Select Diameter from BallsProbes where Remark =6')
end
EXEC(#query)

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

Assign to a T-SQL variable from a CASE statement

I'd like to assign some variables inside a query that uses CASE statements for it's columns. Not quite sure how to do this, having trouble finding the right syntax.
This is what I have so far, but it's got syntax errors.
-- set #theID and #theName with their appropriate values
select top (1)
#theID = (Case when B.ID IS NULL then A.ID else B.ID END) ,
#theName = (Case when B.Name IS NULL then A.Name else B.Name END)
from B left join A on A.ID = B.ID where ...
What's the correct place/way to stick those variables in there?
The example you've given should work. You can assign to variables from a case statement. Just pretend that the entire CASE..WHEN..THEN..ELSE..END block is a field. Here is a generic example:
declare #string1 nvarchar(100) = null
declare #string2 nvarchar(100) = null
select
#string1 = case when 1=1 then 'yes' else 'no' end
,#string2 = case when 1=0 then 'yes' else 'no' end
print 'string1 = ' + #string1
print 'string2 = ' + #string2
Gives:
string1 = yes
string2 = no
Can you tell us what specific error(s) you are getting?
You could probably do this more easily using ISNULL or COALESCE:
select top (1)
#theID = ISNULL(B.ID, A.ID),
#theName = ISNULL(B.Name, A.Name),
from B left join A on A.ID = B.ID where ...
DECLARE #SmallBlindSeatId INT
DECLARE #BigBlindSeatId INT
DECLARE #DealerSeatId INT
DECLARE #NextTurn INT
SELECT #DealerSeatId=( CASE WHEN BlindsInfo=1 THEN SeatId ELSE #DealerSeatId END ),
#SmallBlindSeatId=( CASE WHEN BlindsInfo=2 THEN SeatId ELSE #SmallBlindSeatId END),
#BigBlindSeatId=( CASE WHEN BlindsInfo=3 THEN SeatId ELSE #BigBlindSeatId END),
#NextTurn=( CASE WHEN NEXTTURN=1 THEN SeatId ELSE #NextTurn END)
FROM ABC WHERE TESTCASEID=1
PRINT(#DealerSeatId)
PRINT(#SmallBlindSeatId)
PRINT(#BigBlindSeatId)
PRINT (#NextTurn)