I need to write a query that increments a value in a table by 3 when run.
I would like to do something like this but this doesn't work.
UPDATE table
SET value = (SELECT value
FROM table
WHERE condition = true) + 3
WHERE condition = true
As in the title this is a DB2 database, any ideas?
EDIT: Actually this does work, could also do the + 3 in the select. I just had some stuff in the wrong place with the casting I had to do
Thanks in advance
I think what you are looking for is simply
UPDATE table SET value = value + 3 WHERE condition = TRUE
Does that work?
If you want all rows where condition = true to have (for example) 3+ the max value of any row for which the condition is true, use this:
UPDATE table SET value =
(
SELECT MAX(value)
FROM table WHERE condition = true
) + 3
WHERE condition = true
Related
I have a table with 5 rows. An ID column, 2 varchar columns, an isValid Column and a ValidationFailure column. The data for 2 columns are updated with data from an excelsheet through code. The data types while inserting for 2 columns are varchar. I now have to check if these 2 columns match MONEY type and if there is an issue update column isValid to false with ValidationFailure column a concatenation of the column names that fail the test.
I am using an update statement with a where clause. But I am unable to have a concatenation of the column names that fail the condition.
I am using the following update statement :
update t
set t.isValid = 0
from table t
where IsNumeric(t.col1+'e0') = 0 or IsNumeric(t.col2+'e0') = 0
How can I update t.ValidationFailure column with the column names that failed the condition?
If both fails then the column value can be col1,col2
This can be achieved with a CASE
update t set
t.isValid = 0,
t.ValidationFailure =
case
when IsNumeric(t.col1+'e0') + IsNumeric(t.col2+'e0') = 0 then 'both'
when IsNumeric(t.col1+'e0') = 0 then 'col1'
when IsNumeric(t.col2+'e0') = 0 then 'col2'
else 'unknown'
end
from table t
where IsNumeric(t.col1+'e0') = 0
or IsNumeric(t.col2+'e0') = 0
In Postgres 9.3 when a field in a table has a null value the following expression doesn't work:
update table_statatistic set members = members + 1 WHERE user_id = $1;
However when the field has an integer value then this query increments it by 1 without a problem.
The questions are:
Why is this happening.
How to fix it.
you need to use coalesce for checking null values
update table_statatistic set members = coalesce(members, 0) + 1 WHERE user_id = $1
Use COALESCE() instead:
The COALESCE function returns the first of its arguments that is not null. Null is returned only if all arguments are null. It is often used to substitute a default value for null values when data is retrieved for display
UPDATE table_statatistic SET members = COALESCE(members,0) + 1 WHERE user_id = $1;
I have a table that contains a column that I need to update if the length of that value is equal to 1, the thing is that if this is true I need to get that value and concatenate a 0 before it, for example:
If the value of the column is equal to "5" I need to update that row to "05", I need to do this for all the rows that match this criteria.
I tried this:
UPDATE WS
SET WS.used_brand=CONCAT('0',(Select WS.used_brand FROM WS)) WHERE LEN(WS.used_brand) = 1;
It doesn't work because of the inner select, how can I fix this?.
Thanks.
I think this does what you want:
UPDATE WS
SET WS.used_brand = CONCAT('0', WS.used_brand)
WHERE LEN(WS.used_brand) = 1;
Note: Many databases support LPAD() or a similar function for padding values on the left.
In SQL Server, you would more likely write this as:
UPDATE WS
SET WS.used_brand = '0' + WS.used_brand
WHERE LEN(WS.used_brand) = 1;
You don't need a nested SELECT statement - you can reference the column just like this:
UPDATE WS
SET WS.used_brand=CONCAT('0', WS.used_brand)
WHERE LEN(WS.used_brand) = 1;
Ive seen a few examples on here to work out how to add a percentage to a value in sql, but they don't match if I actually do the calculation.
Ive tried "Value + (value * percentage)"
Any other examples I can try, I have a value column and a percentage column which will be used to work out the increased/decreased amount.
So you need:
SELECT Value,Percentage,OtherColumns.....,VALUE + (value*percentage)/100 as newValue
FROM YourTable
If you need an update, it works the same way:
UPDATE YourTable
SET VALUE = VALUE + (value*percentage)/100
You can add where clause to filter only a desired records
If you need to add a percentage to an existing value add 1 to the percentage and multiply by the value.
Update table set value = value*(1+45%)
100*110% = 110...
9*(1+5%) = 9.45...
8*(1+100%) = 16
2*(1+0%) = 2
This assumes the value in the table is decimal to begin with if integer, then you will end up with some rounding issues.
I am working on a select statement in SQL and am running into issues trying to create a where clause that includes a case statement or an if else statement. I want to select records based on the value of a variable. If the variable is 'True' then only return records from the select statement where a column is null. If the variable is not 'True' then return all records regardless if that columns is null.
Any tips on how to do this?
Below is a simple example of what i am trying to do:
declare #option1 as varchar(5)
--This can be True or False so to test i just put the Set option below
set #option1 = 'True'
Select a,b,c,d...
from ...
where d = case when #option1 = 'True' then NULL End
This is the part where i do not know what to do. I only need to filter out the records if the variable is 'True' so not sure what to put in the else section of the case.
You can't test for d = NULL as your CASE statement does because that will always return false since NULL is not equal to NULL (unless you set ANSI_NULLS to 'off').
The simplest thing to do would be to change the WHERE clause to this:
WHERE #option1 = 'False' OR d IS NULL
If you prefer to use a CASE statement for some reason, you can write it like this:
WHERE 1 = CASE WHEN #option1 = 'False' THEN 1
WHEN #option1 = 'True' AND d IS NULL THEN 1
ELSE 0
END
This:
UPDATE: PinnyM has straightened me out on this. I am leaving my embarrassing logically flawed argument here for the education of the masses. The solution I propose below after "Try this" is certainly still valid, but PinnyM's solutions is by far more elegant and should be used.
WHERE #option1 = 'False' OR d IS NULL
Will always return all the results given his current select statement (assuming #Option1 is simply a flag parameter passed in).
Try this:
SELECT a, b, c, d
WHERE
-- Returns only rows where d is null (if #Option1 is True)
(#Option1 = 'True' AND d IS NULL)
OR
-- returns all the rows (if #Option1 is False)
(#Option1 = 'False')