Codigo varchar
Cantidad float
Cantidad2 float
Cantidad3 float
I have a table in SQL Server which gives me the following result
Codigo Cantidad Cantidad2 Cantidad3
00428 0 0 32
Now I need to add 1 to the float columns but that they are different from 0, this is my query but even if they have a value of 0 if they add the number
update SIPGProyeccion set Cantidad=(Cantidad+1),Cantidad=
(Cantidad2+1),Cantidad3=(Cantidad3+1)
where Codigo= '00428'
and Cantidad<>0
and Cantidad2 <>0
and Cantidad3<>0
Details:
where
, it must be valid for anythingCantidad
that is different from 0case
to determine if we will add 1 or 0 based on the condition of eachCantidad
.If you want to add 1 only to the columns that do not have 0, you can do it with the following query:
As you will see, the use of IIF allows us to add 0 or 1 according to the current value of the corresponding column.
In the where, the condition that you had to check that all the values were 0 was eliminated, since this would cause that only if the three columns were 0, they would be increased by 1.