Updating column B value case when conditional statement is true - sql

I need to update Column C value to 1.5 when Column A = Column B Else it should be 4 in SQL Server.
I wrote something like this
UPDATE MyTable
SET Column C = 1.5 CASE WHEN Column A = Column B ELSE 4 END NewColumn.
I have never used UPDATE and CASE WHEN statement.

The correct syntax is:
UPDATE MyTable
SET C = (CASE WHEN A = B THEN 1.5 ELSE 4 END)

You can also use IIF() as
UPDATE MyTable
SET C = IIF(A=B, 1.5, 4);

Related

Is there a way to create a new column and assign values based on criteria on an existing column in a SQL query?

I want to create a new column in my query output and assign values to it based on existing columns in my input. Let's say i have column A in my input and want to create column B in my query output. How do I add the following logic in a sql select statement?
If A = 1 then set B to x
Else if A = 2 then set B to y
Else if A = 3 then set B to z
Else set B to null
I have to do this for multiple new columns in my output within the same query.
This is a case expression:
select . . .,
(case A when 1 then 'x' when 2 then 'y' when 3 then 'z' end) as b
Something like this?
SELECT A,
CASE
WHEN A = 1 THEN 'x'
WHEN A = 2 THEN 'y'
ELSE 'z'
END AS B
FROM ...

Update Table with Aggregate function in where clause Bigquery

I have TableA with the following data
Name A B Final
Andy 1 1 2
Sam 1 0 2
I want to write an update query which will do the following:
If sum(A+B) <> Final then update row else do not Update
My Query
Update TableA
Set A = Case when Final in (1,2) then 1 else 0 End
Set B = Case When Final = 2 then 1 else 0
where final in (1,2) and **final <> sum(A+B)**
Since we can't use an aggregate function in update where clause I am not sure how to do the last part.
The query should only update row for Sam.
Thanks for you help!
Update TableA
Set A = Case when Final in (1,2) then 1 else 0 End
Set B = Case When Final = 2 then 1 else 0
where final in (1,2) and final <> A+B

Changing when to if statement in SQL server

How do I change the following code to an if statement that returns a boolean 0 or 1 value? My end results I would like to have, is one column listing the interest rate of 2, and my results column with a 0 or 1 if the condition is true.
(Case when new_interestratevariability = 2
and (new_interestrateindex = 1 or new_interestrateindex = 2 or new_interestrateindex = 3 or new_interestrateindex = 4 or new_interestrateindex = 6)
and new_crms_dt = #Curr_Date
then 0 else 1 end) as CIEDIT_VAL_96,
Currently, I am getting something like below:
Results Table
To filter rows, use a Where clause. The Case statement in the Select clause will modify the value shown on the row.
Select *
from table
Where new_interestratevariability = 2
and new_interestrateindex IN (1,2,3,4,6)
and new_crms_dt = #Curr_Date
Found my answer, it was as simple as adding "not in" instead of just "in". Thanks everyone
(Case when new_interestratevariability = 2
and (new_interestrateindex not in(1,2,3,4,6))
and new_crms_dt = #Curr_Date
then 1 else 0 end) as CIEDIT_VAL_96,

How to send update batch with statements simultaneously in SQL

BEGIN TRANSACTION
UPDATE table_x
SET Part = 8
WHERE A = 2 AND B = 2 AND C = 1 AND Code = 'X'
UPDATE table_x
SET Part = 0
WHERE A = 2 AND B = 2 AND C = 1 AND Code = 'Y'
UPDATE table_x
SET Part = 2
WHERE A = 2 AND B = 2 AND C = 1 AND Code = 'Z'
COMMIT TRANSACTION
Basically we have three rows that together has to have a value total of 10 (as you can see now it is 8+0+2 = 10). I have a trigger that checks so the value always should be 10 when updating or inserting new values, but as for now it wont be able to update since it sends the first UPDATE "WHERE A = 2 AND B = 2 AND C = 1 AND Code = 'X' " to check, and the sum will in most cases not go through since it will be either more or less than 10.
What I want is to send all these UPDATES at the same time so the values changes together. Is there any way to make that happen?
SQL Server is actually a bit smarter about triggering, so one statement should work:
UPDATE table_x
SET Part = (CASE Code WHEN 'X' THEN 8 WHEN 'Y' THEN 0 ELSE 2 END)
WHERE A = 2 AND B = 2 AND C = 1 AND Code IN ('X', 'Y', 'Z');
The trigger itself has to be really smart about checking inserted values rather than values in the table, but this is doable.
Alternatively (and possibly the better solution under some circumstances) is to disable the trigger for the updates. This has the advantage that the trigger code does not need to change.
Is this what you want to do:
UPDATE table_x
SET Part = CASE WHEN Code = 'X' THEN 8
WHEN Code = 'Y' THEN 0
WHEN Code = 'Z' THEN 2
END
WHERE A = 2 AND B = 2 AND C = 1 AND Code IN ('X','Y','Z')

Using IF..ELSE in UPDATE (SQL server 2005 and/or ACCESS 2007)

I need to set a query like below:
UPDATE XXXXXX
IF column A = 1 then set column B = 'Y'
ELSE IF column A = 2 then set column C = 'Y'
ELSE IF column A = 3 then set column D = 'Y'
and so on and so forth...
I am able to do this using multiple queries but was wondering, if I can do it in just 1 statement instead.
this should work
update table_name
set column_b = case
when column_a = 1 then 'Y'
else null
end,
set column_c = case
when column_a = 2 then 'Y'
else null
end,
set column_d = case
when column_a = 3 then 'Y'
else null
end
where
conditions
the question is why would you want to do that...you may want to rethink the data model. you can replace null with whatever you want.
Yes you can use CASE
UPDATE table
SET columnB = CASE fieldA
WHEN columnA=1 THEN 'x'
WHEN columnA=2 THEN 'y'
ELSE 'z'
END
WHERE columnC = 1