Get next value (not null) in the same column in SQL - sql

I have a SQL query which return the lines with Ids and Name,
There are some rows which are NULL, so i used ISNULL('row', -1) so set those null rows to -1.
ISNULL(AORL_AOR_Id,-1) as 'AORL_AOR_Id',
But for better development and those rows in the same column can exist with the same value, I would like to ask that is there a way to get the next not null value in the same column to set to the current null value ?
So in my case, those rows with -1 will get the value of 28735.

Related

Postgres: How to find rows in which a specific column has 'empty' values?

I need to find rows which have a empty values in a column after a CSV import.
The column is an INTEGER column, hence the
...
where col = ''
doesn't work for me.
You can check for empty values using
where col is null
If you want to select null as 0 (or any other default value) use coalesce
select coalesce(max(col), 0)

Inserting incremented number where no identity / increment exists

I am working with a table which has a number assigned to each row (nulls not allowed) in the 'entryorder' column
1
2
3
In order to insert a row I can do a ##rowcount and assign the result to a variable+1 then use that in the insert statement
Is this the safest way ?
if you are not able to change column properties to identity/autoincrement, then use max function to get max value of the column and add one to it for the new row. instead of using ##rowcount

Update Column value for all rows in Table where Column Value Is Null?

Still learning SQL-Fu, and trying to figure out how to do a simple update on my Table (ex. [TABLE1])to where all rows that have a [COST] column value of NULL are updated to a [COST] value of 0.00.
Can anyone show me how this is properly done? I've found examples for how to update EVERY row value for the column, but haven't quite been able to piece together the WHERE condition in a functional way.
You can test for a NULL value in a column using IS NULL.
UPDATE Table1
SET cost = 0
WHERE cost IS NULL;

NOT EQUAL operator != not working as expected in Oracle [duplicate]

This question already has answers here:
Not equal <> != operator on NULL
(10 answers)
Closed 8 years ago.
I have made a new column in my database table:
alter table TABLE add (COLUMN NUMBER(1));
Now I want to select all records where the value of column is not zero, either it’s some other number of empty (null)
SELECT * FROM TABLE WHERE COLUMN != 0;
And by default all columns have empty/null value as this column is recently added so it should return all records but doesn’t return any.
But if a change the value for this column of a record to 0 and run below query it works fine.
SELECT * FROM TABLE WHERE COLUMN = 0;
Please help I want to select all records where the value is not 0.
For NULL values you have to explicitly give IS NULL or IS NOT NULL
SELECT * FROM TABLE WHERE COLUMN <> 0 OR COLUMN IS NULL;
OR
SELECT * FROM TABLE WHERE NVL(COLUMN,1) <> 0
The Optimizer is going to do like in the first case even if you go with NVL()

Update a table based in some conditions

I need to update the DataEHoraInicialDoFeedback column with the value from the DataEHoraInicial column, where the DescricaoDoFeedback is not empty (the field has no null values) and DataEHoraInicialDoFeedback is null or DataEHoraInicialDoFeedback is empty and StatusDoFeedback has the value com Sucesso
UPDATE GestaoDeAlertas
SET GestaoDeAlertas.DataEHoraInicialDoFeedback = GestaoDeAlertas.DataEHoraInicial
WHERE EXISTS
(
SELECT *
FROM GestaoDeAlertas
WHERE DescricaoDoFeedback <> ''
AND (DataEHoraInicialDoFeedback IS NULL OR DataEHoraInicialDoFeedback= '')
AND StatusDoFeedback= 'com Sucesso'
)
In my tests (I have duplicated the table) the above update is updating all rows, as if it is ignoring the where clause.
Your query says:
If there is at least one row in the entire table that satisfies
the specified condition, then do the update (regardless of the values in the current row).
I believe that Dan Bracuk already gave you the right advice (see his comment): use direct WHERE clause.