Increment a nullable column in oracle - sql

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);

Related

null value in column "balance" violates not-null constraint

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';

ISNULL() not working in update of table variable

I have a stored procedure that includes a table variable which updates from other table variables. ISNULL() is not working in the below code. It returns the proper value if there is something in TR.TotalRequestCnt, but NULL if there is no value in the TotalRequest table for this column.
UPDATE #OutputTable
SET TotalRequestCount = ISNULL(TR.TotalRequestCnt, 0)
FROM #TotalRequest TR
JOIN #OutputTable OT
ON TR.Document = OT.Document
Values of Document are identifying INTs (5577,5575, 5574).
#TotalRequest values are all INT:
5577 NULL NULL
5575 NULL NULL
5574 2 1
I have also tried using COALESCE() instead of ISNULL() to no success.
I found the answer.
No update is being performed in the row at all because there are no values so ISNULL never has the chance to fire.

SQL - Setting parameters for Default Column Value in Table

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 to update a column with a "blank" value

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)

NULL value for int in Update statement

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