I want to update balance from another table, with sum function but im getting this error:
null value in column "balance" violates not-null constraint
Any suggestion how can i check to not update null values or to change query so that works?
This is my query:
update apt_profile
set payment_currency_code ='CC',
balance = (select sum(open_amount_total) from fnt_pym_profile where fnt_pym_profile.profile_id =apt_profile.id and status ='OPEN')
where payment_currency_code ='DD';
Use COALESCE.
update apt_profile
set payment_currency_code ='CC',
balance = coalesce(select sum(open_amount_total) from fnt_pym_profile where fnt_pym_profile.profile_id =apt_profile.id and status ='OPEN'), 0)
where payment_currency_code ='DD';
This should 0 the balance.
OR use ISNULL:
update apt_profile
set payment_currency_code ='CC',
balance = ISNULL(select sum(open_amount_total) from fnt_pym_profile where fnt_pym_profile.profile_id =apt_profile.id and status ='OPEN'), 0)
where payment_currency_code ='DD';
Related
I am trying to update two variables in a Oracle server as follows:
UPDATE UserTable
SET user_email='asdf#company.com',
(CASE WHEN reason != '' THEN why_update= 'change email server' END)
WHERE user_id = 123
I want to update why_update column only if the user provided a reason for update otherwise leave the column as it is (which is varchar type and can be NULL).
Or any other better solution?
If you're trying to update multiple columns, you would need to do it like:
UPDATE UserTable
SET user_email = 'asdf#company.com',
why_update = CASE WHEN reason IS NOT NULL THEN 'change email server' END
WHERE user_id = 123;
You would always end up updating both columns - in this case, if reason is null, then why_update would be set to NULL, losing any previous value. If you don't want that to happen, you would need to add in an ELSE why_update clause into the CASE expression.
N.B. I have changed your != '' into IS NOT NULL because an empty string ('') is recognised as being a NULL in Oracle. NULL will never match an equals or not equals check, so your above CASE expression won't work as I suspect you want it to work.
THis will work:
UPDATE UserTable
SET user_email='asdf#company.com',
why_update= CASE WHEN reason != '' THEN 'change email server' else why_update END
WHERE user_id = 123
I have a table with nullable column.
create table testgt(retrycount number(3));
I have to update for the following cases.
If the value is null I should update as 0
and if the value is not null I should increment by 1.
Can a single query handle this?
You can use coalesce():
update testgr
set retrycount = coalesce(retrycount + 1, 0);
Using SQL Server 2012.
I have a column in my member table that is created using the following script:
[EnableMemberWebAccess] [bit] NOT NULL DEFAULT ((1))
I want this column to only have 1 as a default if another column in this same table is a certain value. Let's call this column MemDesc, and I want the EnableMemberWebAccess to be defaulted to 1 when MemDesc = 'Founder', and for it to default to 0 when MemDesc != 'Founder'.
Any help much appreciated!
There is probably no way to achieve a default value that can be changed afterwards. Either you have a value that you insert in the beginning. You will then need to take care of consistency within the application:
ALTER TABLE *table* ADD COLUMN EnableMemberWebAccess bit NULL
UPDATE *table* SET *table.*EnableMemberWebAccess = CAST(CASE WHEN *table*.MemDesc = 'Founder' THEN 1 ELSE 0 END AS bit)
ALTER TABLE *table* ALTER COLUMN EnableMemberAccess bit NOT NULL
Or you have to use a computed column. This will not allow you to change the value of the column except if you change the value of the column it depends on.
Computed column should work for you:
ADD EnableMemberWebAccess AS cast((CASE WHEN MemDesc='Founder' THEN 1 ELSE 0 END) as bit)
How do I update a column value (varchar(20), not null) with a "blank" value?
If you want to update it with NULL you will need to change it to allow NULL. Otherwise update with an empty string "".
Update TableName Set ColumnName='' where [Condtion] // To update column with an enpty values
If there have any int column which you may want to do null
Update TABLE_NAME set name = '',id = cast(NULLIF(id,'') as NVARCHAR)
Is it possible to set NULL value for int column in update statement?
How can I write the update statement in this case?
Assuming the column is set to support NULL as a value:
UPDATE YOUR_TABLE
SET column = NULL
Be aware of the database NULL handling - by default in SQL Server, NULL is an INT. So if the column is a different data type you need to CAST/CONVERT NULL to the proper data type:
UPDATE YOUR_TABLE
SET column = CAST(NULL AS DATETIME)
...assuming column is a DATETIME data type in the example above.
By using NULL without any quotes.
UPDATE `tablename` SET `fieldName` = NULL;
Provided that your int column is nullable, you may write:
UPDATE dbo.TableName
SET TableName.IntColumn = NULL
WHERE <condition>
If this is nullable int field then yes.
update TableName
set FiledName = null
where Id = SomeId