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
Related
UPDATE P
SET NRI_1 = (WI_1 * .8)
FROM AC_Property P
WHERE COALESCE(WI_1,0) <> 0 and RSV_CAT = 'PDNP'
Try with this:
UPDATE AC_Property
SET NRI_1 = (WI_1 * .8)
WHERE COALESCE(WI_1,0) <> 0 and RSV_CAT = 'PDNP'
You don't need to FROM because you are using the same table.
Also if you have a Trigger it will cause the error, because you update more than one (1) row.
Why not just use ?
UPDATE AC_Property
SET NRI_1 = (WI_1 * .8)
WHERE COALESCE(WI_1,0) <> 0 and RSV_CAT = 'PDNP'
I have, as a part of a bigger query, some subqueries that I would like to convert to CASE statements instead.
The subquery looks like this (and works):
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM [F0001].[dbo].[ProdTr] WHERE AcYrPr = '201601' AND ProdTr.TrTp = 1 AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1
However, I don't seem to find any logical way to put this into a CASE-statement.
Any help would be appreciated!
(
SELECT CASE
WHEN SUM(t1.DAm) <> 0
THEN (SUM(t1.DAm) + SUM(t1.StcCst)) * 100 / SUM(t1.DAm)
ELSE 0 /* or whatever you want to have in this case */
END AS 'DG'
FROM [F0001].[dbo].[ProdTr] t1
WHERE t1.AcYrPr = '201601' AND
t1.TrTp = 1 AND
t1.CustNo = '12773'
) AS dg_period_1
I also removed some unneeded parentheses and simplified an operation (x - (y * -1) = x + y)
You could use the following statement with CASE provided you want to return a null when SUM(DAm) is null or 0.
(SELECT CASE
WHEN SUM(DAm) IS NOT NULL and SUM(DAm) <> 0 THEN (((SUM(DAm) - (SUM(StcCst) * -1)) * 100) /SUM(DAm))
ELSE NULL
END AS 'DG'
FROM [F0001].[dbo].[ProdTr]
WHERE AcYrPr = '201601'
AND ProdTr.TrTp = 1
AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1
I have these two case statements and can not for the life of me figure out how to combine them to show in a MSSQL view. Any help would be great.
CASE WHEN [ordertype] = '2' THEN [CommissionAmt1] * - 1 ELSE [CommissionAmt1] END
and
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN 0 ELSE CommissionAmt1 END
Just adding the first case to wherever the CommissionAmt1 is referenced in the second statement.
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN
0
ELSE
CASE WHEN [ordertype] = '2' THEN
[CommissionAmt1] * - 1
ELSE
[CommissionAmt1]
END
END
Or going the other way. It was hard to understand which way the calculation needs to be performed. The only hint was []
CASE WHEN [ordertype] = '2' THEN
(
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN
0
ELSE
CommissionAmt1
END
) * - 1
ELSE
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN
0
ELSE
CommissionAmt1
END
END
Either way, you would be able to save some calculations by sub querying the dependent value.
SELECT
*,
ValueWithDependant=CASE WHEN (Dependant>0) THEN (SomeValue / Dependant) ELSE NULL END
FROM
(
SELECT
X,Y,Z,
Dependant=CASE WHEN SomeValue=1 THEN 1 ELSE 0 END
FROM
SomeTable
)AS DETAIL
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)
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