If Var is certain Value omit the WHERE - sql

Is it possible to omit the WHERE if a variable is a certain value? The following doesn't work and I am struggling to find an answer;
DECLARE #rMonth int, #rYear int, #sID int
SET #rMonth = 0;
SET #rYear = 0;
SET #sID= 0;
SELECT
TCS.bStatus AS jStatus, TCS.ID, TCS.sID, TCS.insDate, TCS.statusLabel, TCS.cID
FROM
TCS
CASE WHEN #rMonth > 0 THEN
WHERE month(insDate) = #rMonth AND year(insDate) = #rYear
END

This is common scenario to include a clause conditionally
check this out:
WHERE
(#rMonth = 0 OR MONTH(insDate) = #rMonth)
AND (#rYear = 0 OR YEAR(insDate) = #rYear)
AND (#sID = 0 OR sID = #sID)
In above query and for each clause, right hand side of "OR" is applied only if left hand side is false. Otherwise whole clause is considered as true and does not filter any row.
Hopefully,by using above trick any complex clause could be written in right side of "OR" rather than a simple equality comparision.

WHERE
(#rMonth <= 0)
OR
(month(insDate) = #rMonth AND year(insDate) = #rYear)

Another two options to achieve the same result:
one - using CASE statement like you wanted initially:
WHERE MONTH(insDate) = CASE WHEN #rMonth > 0 THEN #rMonth ELSE MONTH(insDate) END
AND YEAR(insDate) = CASE WHEN #rYear > 0 THEN #rYear ELSE YEAR(insDate) END
AND sID = CASE WHEN #sID > 0 THEN #sID ELSE sID END
and another - using ISNULL and NULLIF functions:
WHERE MONTH(insDate) = ISNULL(NULLIF(#rMonth, 0), MONTH(insDate))
AND YEAR(insDate) = ISNULL(NULLIF(#rYear, 0), YEAR(insDate))
AND sID = ISNULL(NULLIF(#sID, 0), sID)

Related

SQL update to make a toggle

I want to make a toggle request as defined there T-SQL: Using a CASE in an UPDATE statement to update certain columns depending on a condition
I did this :
Update capteur
join smartparking_reference on(smartparking_reference.id_capteur = capteur.id_capteur)
set (CASE WHEN capteur.valeur != 0 then capteur.valeur = 0 and last_value_date = now() END)
where smartparking_reference.id_ref = 3;
But always a syntax error. So what did i miss ?
Apparently you want to update capteur.valeur only when it is nonzero and set it to zero. Try this simpler statement:
MySql style
Update capteur
join smartparking_reference on(smartparking_reference.id_capteur = capteur.id_capteur)
set valeur = case when valeur = 0 then 1 else 0 end, last_value_date = now()
where smartparking_reference.id_ref = 3;
Sql Server style
Update capteur
set valeur = case when valeur = 0 then 1 else 0 end, last_value_date = getdate()
from smartparking_reference
where smartparking_reference.id_capteur = capteur.id_capteur
and smartparking_reference.id_ref = 3;
I also guess your update statement has the wrong syntax
UPDATE c
SET valeur = 0, last_value_date = now()
FROM capteur c
INNER JOIN smartparking_reference AS sr ON
sr.id_capteur = c.id_capteur
WHERE sr.id_ref = 3 and c.valeur != 0
UPDATE capteur
SET capteur.valeur=(
CASE
WHEN capteur.valeur != 0 THEN
0 ELSE 'another value'
END
),last_value_date = now ()
FROM capteur
INNER JOIN smartparking_reference ON
smartparking_reference.id_capteur = capteur.id_capteur
WHERE
smartparking_reference.id_ref = 3;

SQL Server : IF Condition in WHERE clause

I have a bit parameter
#IsInRetry
If it is false I have to set the where condition to
attempts = 0
else if it is true I have to set the where condition to
attempts > 0
How could this be done?
Try this way:
( (#IsInRetry = 0 and attempts = 0) or (#IsInRetry = 1 and attempts > 0) )
Try this:
WHERE (attempts = #IsInRetry or (#IsInRetry = 1 and attempts > 0))
Try this, should work also for negative attempts :-)
#IsInRetry = (attempts > 0)
Greetings.
This:
if #IsInRetry = 0x0
BEGIN
SELECT * FROM dbo.tbl
WHERE attempts = 0
END
ELSE
BEGIN
SELECT * FROM dbo.tbl
WHERE attempts = 1
END

IF ELSE CONDITION IN SQL WHERE CLAUSE

MY QUERY IS
DECLARE #AutoApprove BIT
SET #AutoApprove = (
SELECT AutoApprove
FROM dbo.CommentBox_Setting
WHERE UserModuleID = #myModuleID
AND PortalID = #portalID
AND CultureCode = #cultureCode
)
From this i will get whether 1 OR 0 (TRUE OR FALSE) furthermore i have
SELECT * FROM ComentBox_Comment
WHERE UpperModuleID = #UpperModuleID
AND ModuleID = #myModuleID
AND portalID = #portalID
AND cultureCode = #cultureCode
AND //Here i need to check condition
(IF(#AutoApprove=0){ THEN isapprove=1}else {do not check})
Note here isapprove is table filedName
I know ,i can do this with long query i need short and easy way.
Help me out.
Try something like
AND CASE WHEN #AutoApprove=0 THEN isapprove ELSE 1 END = 1
This will check isapprove = 1 if #AutoApprove = 0, or 1=1(ignore) otherwise.
CASE (Transact-SQL)

SQL Boolean Vars in Where stament

I am making an rdl report and I have three check-boxes that if checked need to alter my WHERE statement. If none are checked the result should only match by date range. If one or more are checked it needs to return the fields that match the variables corresponding string.
WHERE
(EffectiveDate BETWEEN #StartDate AND #EndDate)
AND (((#IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR #IncludeSEWPrefix = 0)
AND ((#IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR #IncludePAWPrefix = 0)
AND ((#IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR #IncludeRPLPrefix = 0))
My code so far works when none are checked and when one is checked, but returns nothing when more than one check-box has been checked. So to try and fix this I altered the code to this
WHERE
(EffectiveDate BETWEEN #StartDate AND #EndDate)
AND ((((#IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR #IncludeSEWPrefix = 0)
OR ((#IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR #IncludePAWPrefix = 0)
OR ((#IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR #IncludeRPLPrefix = 0)))
Which resulted in all rows being returned no matter what was selected. Can someone tell me where I am going wrong?
I believe this is the correct rearrangement. Trickier problem than it first appears. The issue was seperating lines like ((#IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR #IncludeSEWPrefix = 0) with AND meant that if two includes were true, a row would need to have both PrefixId's, which can't happen. And if you separated them with OR, then having just one include false, means that every row will pass. So instead, check that a row has the prefix of any that are included, otherwise all includes have to be off.
WHERE EffectiveDate BETWEEN #StartDate AND #EndDate
AND
(
(#IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR
(#IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR
(#IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR
(#IncludeSEWPrefix = 0 AND #IncludePAWPrefix = 0 AND #IncludeRPLPrefix = 0)
)
Try this
WHERE
(EffectiveDate BETWEEN #StartDate AND #EndDate)
AND
(
(#IncludeSEWPrefix = 1 AND PrefixId = 'SEW' OR #IncludeSEWPrefix = 0) AND
(#IncludePAWPrefix = 1 AND PrefixId = 'PAW' OR #IncludePAWPrefix = 0) AND
(#IncludeRPLPrefix = 1 AND PrefixId = 'RPL' OR #IncludeRPLPrefix = 0)
)
You have more parenthesis than needed, it does not hurt but just be aware.
Maybe this can help:
WHERE (EffectiveDate BETWEEN #StartDate AND #EndDate)
AND ( ((#IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR (#IncludeSEWPrefix = 0 AND #PrefixId <> 'SEW'))
OR ((#IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR (#IncludePAWPrefix = 0 AND #PrefixId <> 'PAW'))
OR ((#IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR (#IncludeRPLPrefix = 0 AND #PrefixId <> 'RPL'))
)

Conditional operator in Transact-sql

Is there a way to do this shorter, for instance using some sort of conditional operator in Transact-sql?
IF #ParentBinaryAssetStructureId = -1
BEGIN
SET #ParentBinaryAssetStructureId = NULL
END
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId = #ParentBinaryAssetStructureId
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
USE NULLIF()
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId = NULLIF(#ParentBinaryAssetStructureId,-1)
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
The ternary (conditional) operator in c like languages:
x = doSomething ? 5 : 7
would be written like this in SQL:
SELECT #x = CASE WHEN #doSomething = 1 THEN 5 ELSE 0 END
There can be multiple cases (when clauses):
SELECT #x = CASE WHEN #doSomething = 1 THEN 5 WHEN #somethingElse = 1 THEN 20 ELSE 0 END
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId =
CASE ParentBinaryAssetStructureId
WHEN -1 THEN NULL
ELSE ParentBinaryAssetStructureId
END
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
Give that a whirl