How to set optional parameter for int type variable - sql

Have two variable StatusTypeTestID and StatusTestID want to set them as optional in where clause as like bellow,but optional option not work for int variable.
Note: default value for int variable is 0
DECLARE #StatusTypeTestID as int
SET #StatusTypeTestID = 1
DECLARE #StatusTestID as int
SET #StatusTestID = 0
select *
from LiveCustomerStatus
where (StatusType=#StatusTypeTestID
and (Status = #StatusTestID or #StatusTestID is null))

As you were already trying, you can check if your parameters have been set as null or if they match the correspondent field.
This way you can leave a parameter as null if you want it to be optional, not affecting the result.
select *
from LiveCustomerStatus
where (#StatusTypeTestID is null or StatusType=#StatusTypeTestID)
and (#StatusTestID is null or Status = #StatusTestID)
option(recompile)
I have added a option(recompile) clause that will force SQL Server to recompile the query at every execution. This way it will use the appropriate indexes to optimize it depending of the value of the parameters (wether they are null or not).

You can comment both set statements and below query will work:
select * from LiveCustomerStatus
where (#StatusTypeTestID is null or StatusType=#StatusTypeTestID)
and (#StatusTestID is null or Status = #StatusTestID)

Use CASE Statement in WHERE clause :
SELECT *
FROM LiveCustomerStatus
WHERE StatusType = CASE WHEN ISNULL(#StatusTypeTestID,'') = '' THEN
StatusType ELSE #StatusTypeTestID END
AND Status = CASE WHEN ISNULL(#StatusTestID,'')= '' THEN Status ELSE
#StatusTestID END

Related

How to put IF condition in where section of select command in SQL?

I declare some filters on a form and pass them into my SP in SQL for running a select command and this is how I did that, but it didn't work:
DECLARE #Confirm_Filter AS BIT=NULL,
#ReciveDate_Filter AS NCHAR(10) = NULL
....
select * from .......
where
(#Confirm_Filter IS NOT NULL AND InterviewConfirm = #Confirm_Filter)
AND
(#ReciveDate_Filter IS NOT NULL AND ReciveDate BETWEEN GETDATE() AND #ReciveDate_Filter)
and in the where section I want to do:
where
if #Confirm_Filter IS NOT NULL Then (Select * from ... where InterviewConfirm = #Confirm_Filter)
AND
if #ReciveDate_Filter IS NOT NULL Then (select * from .... where ReciveDate BETWEEN GETDATE() AND #ReciveDate_Filter)
and I know above form is totally mistake but how could I do it?
Your problem is called dynamic search conditions. It is solved either by using dynamic SQL, or the following pattern (which is similar to your first version, but with a few changes):
select * from .......
where
(#Confirm_Filter IS NULL OR InterviewConfirm = #Confirm_Filter)
AND
(#ReciveDate_Filter IS NULL OR ReciveDate BETWEEN GETDATE() AND #ReciveDate_Filter)

Update multiple values using IS NULL condition

I just finished migrating an Access database to the SQL Server and I need to fix the Yes/No values to be defaulted to No as their data type conversion is set to bit.
I do this by setting the default value to 0.
However, I want to execute a query to do that as well, but there are multiple bit rows.
I know how to use multiples with the SET command, but how do we use multiple with WHERE? What is the proper way of structuring it?
UPDATE sometable SET
[isConditionOnePassed] = 0
, [isSecondPassed] = 0
, [isThirdPassed] = 0
WHERE [isConditionOnePassed] IS NULL, [isSecondPassed] is NULL, [isThirdPassed] IS NULL
Or is it with an AND? Like boolean logic?
UPDATE sometable SET
[isConditionOnePassed] = 0
, [isSecondPassed] = 0
, [isThirdPassed] = 0
WHERE [isConditionOnePassed] IS NULL and [isSecondPassed] is NULL and [isThirdPassed] IS NULL
Hmmm . . . If you want to set NULL values to another value, you can use COALESCE():
UPDATE sometable
SET isConditionOnePassed = COALESCE(isConditionOnePassed, 0),
isSecondPassed = COALESCE(isSecondPassed, 0),
isThirdPassed = COALESDCE(isThirdPassed, 0)
WHERE isConditionOnePassed IS NULL OR isSecondPassed is NULL OR isThirdPassed IS NULL;
Seems like what you really need is an ISNULL or CASE expression. Here is an example with both:
UPDATE dbo.SomeTable
SET isConditionOnePassed = ISNULL(isConditionOnePassed,0),
isSecondPassed = CASE isSecondPassed WHEN 1 THEN 1 ELSE 0 END;

SQL : Update value when the value in the database is null

I know this is already asked question and possible to be close.
But i really want a answer, I already searched through the internet, Read documentations, Blogs, and Question to SO.
This is my Query so Far,
declare #count numeric
select #count = (select count(1) from E496_TitleReference a where
exists (select 1 from #tempTransactions b where a.EPEB_RoD = b.tEPEB_RoD and
a.EPEB_ENO = b.tEPEB_ENO and a.EPEB_ID = b.tEPEB_ID and a.Title_Seq = b.tTitle_Seq))
update E496_TitleReference
set PrintStatus = '{0}',Is_AESM=isnull(-1,Is_AESM)
from E496_TitleReference a where
exists (select 1 from #tempTransactions b where a.EPEB_RoD = b.tEPEB_RoD and
a.EPEB_ENO = b.tEPEB_ENO and a.EPEB_ID = b.tEPEB_ID and a.Title_Seq = b.tTitle_Seq)
if ##rowcount <> #count
begin
rollback tran
Print "Error: There is an error on table E496_TitleReference."
return
end
go
For eg, In my table in Database i have column name Is_AESM, In Is_AESM column it have 4 values.
Is_AESM
NULL
NULL
-1
-2
Something like this.
Now when i run my script, it has no problem when i run it,
Is_AESM=isnull(-1,Is_AESM)
In this query it will detect if Is_AESM is null, it will update Is_AESM = -1 if not it will retain the value.
Now my problem is, if my query detect Is_AESM has a null value, it will update all the value to -1.
Is_AESM
-1
-1
-1
-1
The result is something like that. Now i want is update only the null value not all the value in column Is_AESM.
I think this query is wrong Is_AESM=isnull(-1,Is_AESM).
Any ideas will be a big help.
You may try with coalsece() function
update E496_TitleReference
set PrintStatus = '{0}',Is_AESM=coalsece(Is_AESM,-1)
from E496_TitleReference a where
exists (select 1 from #tempTransactions b where a.EPEB_RoD = b.tEPEB_RoD and
a.EPEB_ENO = b.tEPEB_ENO and a.EPEB_ID = b.tEPEB_ID and a.Title_Seq = b.tTitle_Seq)
you need to replace order of parameters.
Is_AESM=isnull(Is_AESM, -1)
You can use COALSECE function. It returns the first non-null entry from the given list. So:
Is_AESM= COALSECE(IS_AESM,-1)
This will return IS_AESM value if it is not null (since it is the first non-null value)
Else if IS_AESM is NULL then it returns -1 (since it is the non-null value)

CASE Statement in WHERE statement (equal to and not equal to)

I have the following query where I'm trying to return different values from a table based off the values of a bit variable.
Is there a way I can substitute the where condition to get that to work?
DECLARE #isAggregate bit = 0
SELECT
*
FROM
Fields
WHERE
FieldType
CASE
WHEN #isAggregate = 1 THEN = 'Aggregate'
WHEN #isAggregate = 0 THEN <> 'Aggregate'
END
You can use boolean logic, case expression will not work in this way :
where (#isAggregate = 1 and FieldType = 'Aggregate') or
(#isAggregate = 0 and FieldType <> 'Aggregate')

Update of column happens even though I don't want it to happen

I've got a case statement:
UPDATE
Answer
SET
AnswerID = #AnswerID,
AnsweredBy = CASE WHEN LEN(#AnsweredBy) > 0 THEN #AnsweredBy END
Even when #AnsweredBy is NULL it still sets the AnsweredBy column to null.
I've tried to test for null as well by doing this:
UPDATE
Answer
SET
AnswerID = #AnswerID,
AnsweredBy = CASE WHEN #AnsweredBy IS NOT NULL THEN #AnsweredBy END
Meaning I do not want to update the answeredby column unless there is a value.
Even in my C# code I do not pass a value to my stored procedure:
if (answeredBy.Length > 0)
cmdSelect.Parameters.Add("#AnsweredBy", SqlDbType.VarChar).Value = answeredBy;
unless there is a value. And in my sproc I default that column variable to null:
#RunoffAnswerID bigint,
#AnswerID varchar(3)=NULL,
#AnsweredBy varchar(50)=NULL,
So my question is how do I perform the rest of my updates, because there are about 5-10 more columns but only update the answeredby if there is a value in #AnsweredBy?
SET AnsweredBy = ISNULL(#AnsweredBy, AnsweredBy)
UPDATE
Answer
SET
AnswerID = #AnswerID,
AnsweredBy = CASE WHEN LEN(#AnsweredBy) > 0 THEN #AnsweredBy ELSE AnsweredBy END