IF(
[dbo.tblx.Category] = 'WS' OR [dbo.tblx.Category] = 'SEM',
0,
[dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
)
this returns the entire text and not the "calculation"
can someone help me...what am I doing wrong?
i tried
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000
END AS Metal
Do perhaps insert something before "CASE WHEN"? it is not running
As I understood, you are trying to create a computed column, using value from other table.
I think in this case you have to use a User Defined function in the formula for computed column. If you give more information I can try to write some code for you.
Instead, if you just want to add a column in a query you can use:
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblPTx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000 END AS NAME_OF_YOUR_NEW_COLUMN
Update
DECLARE #myvar INT;
SET #myvar = CASE WHEN ([dbo.tblx.Category] = WS OR [dbo.tblPTx.Category] = SEM) THEN 0
ELSE [dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
END
Related
I was just woking trough some old final exams as I stumbled across this task:
Table:
Create a single SQL query from this table which will check if Art_MWStSatz is 7% and set Art_Markierung to 1 if it is, else set Art_Markierung to 0.
I dont't have any idea on how to solve this. I asked my teacher by she didn't know an answer either.
Some translations:
Artikel = Item
MWSt Satz = vat rate
Markierung = mark
I don't have access to the solution so it all depends in your answers.
Use an UPDATE and a CASE. This assumes that '7%' is a string and not the int '7'
UPDATE table
SET
Art_Markierung = CASE
WHEN Art_MWStSatz = '7%'
THEN 1
ELSE 0
END;
I presume that 1 or 0 is true or false. In this situation, you can use a CASE like this:
UPDATE MyTable
SET Art_Markierung = CASE Art_MWStSatz
WHEN '7%' THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END
Assume Art_Markierung and Art_MWStSatz are integer:
UPDATE Artikel
SET Art_Markierung =
CASE
WHEN Art_MWStSatz = 7 THEN 1
ELSE 0
END
I have a created field in an Access database that I am trying to re-create in T-SQL. I am getting some incorrect results and I think it is how I wrote the code. Here is the field in Access that is working correctly:
MCP Actual: IIf([lever]="MCP",[actual usd]*IIf([split flag]="x",[split percent],1))*[Allocation Value]
Here is how I have it coded in SQL:
MCPActual =
CASE
WHEN pbd.Lever = 'MCP' THEN pbd.ActualUSD * CASE WHEN ou.SplitFlag = 'x' THEN ((pbd.ActualUSD * ou.SplitPercent) * pda.AllocationValue) END
ELSE ((pbd.ActualUSD * 1) * pda.AllocationValue)
END
I think you haven't got the END and brackets in the correct sequence try
CASE
WHEN pbd.Lever = 'MCP' THEN pbd.ActualUSD *
(CASE WHEN ou.SplitFlag = 'x' THEN
(pbd.ActualUSD * ou.SplitPercent * pda.AllocationValue )
ELSE
(pbd.ActualUSD * pda.AllocationValue) END)
END
You don't need to multiply by 1 unless you are trying to covert a negative / positive value.
I want to use case after where clause in SQL Server 2008; how can I use it like
select *
from MPR
where
case when #min >= 0
then MainComponent_Id = #mainc and SubComponent_Id = #subcomp
else MainComponent_Id = #mainc and SubComponent_Id = #subcomp and MinorComponent_Id = #minorc
end
end
I am using a stored procedure, and in my stored procedure if minorcomponent_id value is 0 then it will add 1 one else 2 one will work.
I had tried hard but its giving error in near case.
How to resolve it?
Thanks
You don't need to use a CASE statement. You can simplify logic as follows:
select
*
from
MPR
where
MainComponent_Id = #mainc AND SubComponent_Id = #subcomp
AND (#min >= 0 OR MinorComponent_Id = #minorc)
I am using Visual Studio 2008 and am in a report trying to create a query that has a WHERE statement that will return all records, if the #parameter = 0, otherwise I want the query to return only records that have the #paramete.
Here is the statement I envision, but can't seem to get correct:
WHERE (ADVISOR.staffPersonID = CASE WHEN #advisor = 0 THEN **'Do NOT
filter by staffpersonID'** ELSE #advisor END)
I have tried the following code which doesn't work:
CASE WHEN #advisor = 0 THEN ADVISOR.staffPersonID ELSE #advisor END
ADVISOR.staffPersonID is the field I would filter on, if the #parameter had a value other than 0. From what I've researched online, using the field itself as the THEN in the statement should work. It does, unless the table (advisor) has no records that match in it at all.
WHERE
(#advisor <> 0 AND ADVISOR.staffPersonID = #advisor)
OR
(#advisor = 0 )
try this:
WHERE ADVISOR.staffPersonID = #advisor OR #advisor = 0
WHERE ((ADVISOR.staffPersonID IS NULL AND #advisor = 0)
OR (ADVISOR.staffPersonID = CASE WHEN #advisor = 0 THEN
ADVISOR.staffPersonID
ELSE
#advisor END
))
I hate code that looks like its been hacked together. I have just written this:
update table1.dbo.totals
set #FEE = case
when isnull(g.SGROUPS,0) > 1
then #GROUPPRICE * case
when CHARINDEX('JMCG', g.GROUPS) > 0
then (g.SGROUPS - 2)
else (g.SGROUPS - 1)
end
else 0
end
from #GROUPMEM as g
if #FEE < 0
begin
set #GROUPFEE = 0
end
I'm particularly wanting to get rid of that nested CASE. Any suggestions?
Ok so this a little odd but may be cool. It gets rid of your nested case and uses some bitwise operations...
update table1.dbo.totals
set #FEE = COALESCE((g.SGROUPS^1)&1,0) * #GROUPPRICE *
case
when CHARINDEX('JMCG', g.GROUPS) > 0 then (g.SGROUPS - 2)
else (g.SGROUPS - 1)
end
from #GROUPMEM as g
if #FEE < 0
begin
set #GROUPFEE = 0
end
You're probably asking what (g.SGROUPS^1)&1 does... This basically converts g.SGROUPS to one if it has a value, allowing us to use it in the multiplication.
OK, I'm going to play some math games here and take advantage of the fact that y*(x-1)-y = y*(x-2).
EDIT: Realized I had my (SGROUPS-1) vs. (SGROUPS-2) logic backwards and fixed.
update table1.dbo.totals
set #FEE = #GROUPPRICE * isnull(g.SGROUPS-1,0) - case when isnull(CHARINDEX('JMCG', g.GROUPS),0)>0 then g.SGROUPS else 0 end
from #GROUPMEM as g
if #FEE < 0
begin
set #GROUPFEE = 0
end
For no CASE statements at all, try:
update table1.dbo.totals
set #FEE = #GROUPPRICE * isnull(nullif(sign(g.SGROUPS-1),-1),0)
* (isnull(g.SGROUPS,0) - 1 - sign(CHARINDEX('JMCG', g.GROUPS))
from #GROUPMEM as g
if #FEE < 0
begin
set #GROUPFEE = 0
end