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
Related
I am trying to create an update statement on SQL Server to set one of the cards as default and all the others as not, however, if I have 3 cards and card 1 set to default and now I want to set card 2 as default I get all 3 cards set as updated, when I should only set cards 1 and 2. I am currently using the following query:
UPDATE Card
SET
ModifiedDateTimeUtc = #ModifiedDateTimeUtc,
IsDefault = CASE WHEN Id = #CardId THEN 1 ELSE 0 END
WHERE CustomerId = #CustomerId;
I need to modify this query so only the cards with values updated get to set a new modified date/time.
PS, I cannot use triggers on this database (unfortunately) so I need to find a solution using a "simple" SQL statement.
Maybe it could be write in other ways but you can do like this:
UPDATE Card
SET
ModifiedDateTimeUtc =
CASE WHEN Id = #CardId THEN
CASE WHEN IsDefault = 1 THEN ModifiedDateTimeUtc ELSE #ModifiedDateTimeUtc END
ELSE
CASE WHEN IsDefault = 0 THEN ModifiedDateTimeUtc ELSE #ModifiedDateTimeUtc END
END,
IsDefault = CASE WHEN Id = #CardId THEN 1 ELSE 0 END
WHERE CustomerId = #CustomerId;
What should do this?
If the Id is the same with the parameter and the old value, before update, is 1, datetime will be the same, no update will be made, else (means old value = 0) then it will be updated. And the same for old value= 0...
Is this what you want?
UPDATE Card
SET ModifiedDateTimeUtc = #ModifiedDateTimeUtc,
IsDefault = (CASE WHEN Id = #CardId THEN 1 ELSE 0 END)
WHERE CustomerId = #CustomerId AND
IsDefault <> (CASE WHEN Id = #CardId THEN 1 ELSE 0 END);
Usually, I wouldn't repeat a CASE expression in the WHERE. In this case, though, this is the new value for the column, so I think it is a clean expression of the logic.
Is this the most efficient way to update one single column from three different columns?
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET REPORTABLE_AMOUNT = ISNULL(PRINCIPAL,0)
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET REPORTABLE_AMOUNT = ISNULL(INTEREST,0)
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET REPORTABLE_AMOUNT = ISNULL(RAW_DATA_AMOUNT,0)
I am using SQL Server. Thanks!
Looks like COALESCE() would help
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET REPORTABLE_AMOUNT = COALESCE(ROW_DATA_AMOUNT, INTEREST, PRINCIPAL, 0)
REPORTABLE_AMOUNT will then be updated with the first value in the COALESCE() that is not null.
Please try SQL : Case - When - End if you are checking Null for update fields.
Egs:
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET REPORTABLE_AMOUNT =
case when ISNULL(PRINCIPAL,0) <> 0 then PRINCIPAL
case when ISNULL(INTEREST,0) <> 0 then INTEREST
case when ISNULL(RAW_DATA_AMOUNT,0) <> then RAW_DATA_AMOUNT
else 0 --default value
End
Regards
Abdul
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
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
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