SQL UPDATE add to field with MIN/MAX - sql

I have this statement
UPDATE table SET health = health + 1 WHERE name='Tom'
How can I set a MAX for the number field in the statement based off? Pseudo code:
UPDATE table set health = min(health+1, max_health) WHERE name='Tom'

The simplest way is to modify the where:
UPDATE table
SET health = health + 1
WHERE name = 'Tom' AND health < max_health;
Some databases support the least() function:
UPDATE table
SET health = LEAST(health + 1, max_health)
WHERE name = 'Tom' ;
You can also do this using CASE.
However, I think the first method is the simplest.

We can use CASE also
UPDATE table
SET health = (CASE WHEN health+1 < max_health THEN
health+1
ELSE
max_health
END) WHERE name='Tom'
AND IIF --SQL Server
UPDATE table SET health = IIF (health+1 < max_health, health+1, max_health) WHERE name='Tom'

Related

Update multiple values using IS NULL condition

I just finished migrating an Access database to the SQL Server and I need to fix the Yes/No values to be defaulted to No as their data type conversion is set to bit.
I do this by setting the default value to 0.
However, I want to execute a query to do that as well, but there are multiple bit rows.
I know how to use multiples with the SET command, but how do we use multiple with WHERE? What is the proper way of structuring it?
UPDATE sometable SET
[isConditionOnePassed] = 0
, [isSecondPassed] = 0
, [isThirdPassed] = 0
WHERE [isConditionOnePassed] IS NULL, [isSecondPassed] is NULL, [isThirdPassed] IS NULL
Or is it with an AND? Like boolean logic?
UPDATE sometable SET
[isConditionOnePassed] = 0
, [isSecondPassed] = 0
, [isThirdPassed] = 0
WHERE [isConditionOnePassed] IS NULL and [isSecondPassed] is NULL and [isThirdPassed] IS NULL
Hmmm . . . If you want to set NULL values to another value, you can use COALESCE():
UPDATE sometable
SET isConditionOnePassed = COALESCE(isConditionOnePassed, 0),
isSecondPassed = COALESCE(isSecondPassed, 0),
isThirdPassed = COALESDCE(isThirdPassed, 0)
WHERE isConditionOnePassed IS NULL OR isSecondPassed is NULL OR isThirdPassed IS NULL;
Seems like what you really need is an ISNULL or CASE expression. Here is an example with both:
UPDATE dbo.SomeTable
SET isConditionOnePassed = ISNULL(isConditionOnePassed,0),
isSecondPassed = CASE isSecondPassed WHEN 1 THEN 1 ELSE 0 END;

Most effifient way to update one column from 3 columns

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

Oracle SQL Adding IF statement

I have a pretty simple question me thinks. I've been looking on the internet, but haven't been able to find anything. I am trying to add an IF statement basically to my Oracle sql.
UPDATE PS_Z_TREND_NOW_TBL a
SET STATUS = (
SELECT COUNT(SEC.IS_AW_AUTH_NAME)
FROM PS_IS_AW_SECURITY sec
WHERE sec.IS_AW_AUTH_NAME LIKE '%Manager%'
I want to update STATUS so that if COUNT(SEC.IS_AW_AUTH_NAME) is greater than 0 it will insert 'M'. How would I write this?
With Case statements.
UPDATE PS_Z_TREND_NOW_TBL a
SET STATUS = ( CASE WHEN COUNT(SEC.IS_AW_AUTH_NAME) > 0 then 'M'
ELSE null END )
FROM PS_IS_AW_SECURITY sec
WHERE sec.IS_AW_AUTH_NAME LIKE '%Manager%'

sql loop update with multiple rows returned

I've got to update a column value by decreasing the value in the column by a variable.
There are two conditions:
1. where the row count = 1
2. where the row count is more than 1
I've got it set to do the single row count but need help when the query returns multiple rows.
set #rowsCounted = (select COUNT(QuantityA) from Offers where WID = #wId and ND = #nd)
if(#rowsCounted = 1)
begin
set #QuantityAvailable = (select QuantityA from Offers where WID = #wId and ND = #nd)
set #QuantityAvailable = (select #QuantityAvailable - #QuantityAdjusted)
update Offers
set QuantityA = #QuantityAvailable
where WID = #wId and ND = #nd
end
else
begin
select #rowsCounted as rowsCounted -- example of 4 rows with values of = 287,280,288,288
--begin loop as the QuantityA may contain different values
end
If #QuantityAdjusted is constant for the procedure, then you only need one update statement. Use set-based thought constructs rather than procedural-based ones:
update Offers
set QuantityA = QuantityA - #QuantityAdjusted
where WID = #wId and ND = #nd
This will update in a set-based operation, and there is no need to construct your own loop. This is part of what SQL engines are meant to do.

conditional handling of update sql script depending on input parameter

I am new to SQL and using Oracle 11. I need to write a sql script which uses different update command based on whether the input param is null or not null.
I need something like this
['&' followed by the parameter name is the way i see parameters being used in other such
script for our project]
IF &PRG_ID IS NULL
UPDATE PROGRAM_TABLE P SET HANDLED_IND = 'Y' WHERE EXISTS
(SELECT 1 FROM HANDLED_PROGRAM H WHERE H.PROGRAM_ID = P.PROGRAM_ID);
ELSE
UPDATE PROGRAM_TABLE P SET HANDLED_IND = 'Y' WHERE
P.PROGRAM_ID = &PRG_ID AND EXISTS
(SELECT 1 FROM HANDLED_PROGRAM H WHERE H.PROGRAM_ID = P.PROGRAM_ID);
END IF;
Something like (is not tested):
UPDATE PROGRAM_TABLE P SET HANDLED_IND = 'Y' WHERE EXISTS
(SELECT 1 FROM HANDLED_PROGRAM H WHERE H.PROGRAM_ID = P.PROGRAM_ID)
AND (&PRG_ID IS NULL OR P.PROGRAM_ID = &PRG_ID);
But take into account this change may lead to the performance degradation for the case when PRG_ID has definite value.