SQL to delete the value from a column where both columns exist - sql

Is this correct? Will it work with Sqlite as well?
UPDATE table SET oldCol=NULL WHERE (oldCol!=NULL AND newCol!=NULL)

No, you should use is not null:
update tbl set oldcol = null where oldcol is not null and newcol is not null

you can use this also.
UPDATE table SET oldCol=NULL WHERE (oldCol is not NULL AND newCol is not NULL)

Related

In SQL, How to add values after add a new column in the existing table?

I created a table and inserted 3 rows. Then I added a new column using alter. How can I add values to the column without using any null values?
Two solutions.
Provide a default value for the column. This value will be used initially for all existing rows. The exact syntax depends on your database, but will will usually look like ..
this:
ALTER TABLE YourTable
ADD YourNewColumn INT NOT NULL
DEFAULT 10
WITH VALUES;
Add the column with null values first. Then update all rows to enter the values you want.
Like so:
ALTER TABLE YourTable
ADD YourNewColumn INT NULL;
UPDATE YourTable SET YourNewColumn = 10; -- Or some more complex expression
Then, if you need to, alter the column to make it not null:
ALTER TABLE YourTable ALTER COLUMN YourNewColumn NOT NULL;
Why don't you use UPDATE statement:
UPDATE tablename SET column=value <WHERE ...>
WHERE is optional. For instance in T-SQL for table:
I can update column NewTestColumn by this statement:
UPDATE [dbo].[Table] SET [NewTestColumn] = 'Some value'
Suppose you have a Employee table with these columns Employee_ID, Emp_Name,Emp_Email initially. Later you decide to add Emp_Department column to this table. To enter values to this column, you can use the following query :
Update *Table_Name* set *NewlyAddedColumnName*=Value where *Columname(primary key column)*=value
Example update TblEmployee set Emp_Department='Marketing' where Emp_ID='101'
I think below SQL useful to you
update table_name set newly_added_column_name = value;
update table_name
set new_column=value
Update table_name set column_name = value where 'condition';
suppose emp is the table and Comm is the new column then fire the below query .
update emp set Comm=5000
For Microsoft SQL (T-SQL):
UPDATE TABLE_NAME SET COLUMN_NAME=10;
here 10 means it will set all values by default to 10

How to insert a value into all cells under a column name

In my SQL table there is a column named IsApproved and it's all NULL. I want to turn them to 'TRUE'. I wrote this SQL statement but it didn't work :
INSERT INTO [persondb].[dbo].[Person] (IsApproved) VALUES ('True')
How can I make this work? Thanks.
update the table with the true value
update table [persondb].[dbo].[Person]
set IsApproved = 'True' where IsApproved is null
you need to update it not insert:
update [persondb].[dbo].[Person] set IsApproved ='True' -- or 1, depends on the field type
where IsApproved is null
Just try with this following one.
select IsNull(IsApproved,'true') from tablename.
(or)
update [persondb].[dbo].[Person] set IsApproved ='True' where IsApproved is null
Hope this will help you.
You can use ISNULL If you only want to show It as result (not to change in table) In following:
SELECT ISNULL(IsApproved, 'True')
If you want to change It in table you should use UPDATE.
UPDATE TABLE [persondb].[dbo].[Person]
SET IsApproved = 'True'
WHERE IsApproved IS NULL

SQL trigger, check if certain value was entered

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
Now what to check:
If inserted value1 = null, change it to 0
How to do it via trigger? I googled for examples and I have never ever done a trigger, so it is rather confusing.
So far got only this:
CREATE TRIGGER testTrigger
ON myTable
AFTER INSERT, UPDATE, DELETE
You can add default value . This is how it's done for a new column. For existing one you should add constraint. Check Update 2
ALTER TABLE table_name
ADD column1 int NOT NULL DEFAULT(0)
Add a column with a default value to an existing table in SQL Server
UPDATE:
To set default value, you should update NULL values at first.
UPDATE table_name
SET column1 = 0
WHERE column1 IS NULL
UPDATE 2:
Try adding constraint
ALTER TABLE table_name
ADD CONSTRAINT DF_column1 DEFAULT 0 FOR column1
You could write this in the trigger:
UPDATE T SET value1 =0
FROM table_name T
JOIN INSERTED I
ON T.<id>=I.<id>
WHERE I.value1 is null
INSERTED table which is accessible only within trigger will store the values that have inserted..
use ISNULL on the INSERTED value
SELECT ISNULL(INSERTED,0) FROM INSERTED

SQL - how can I copy a column and its data to a new column in the same table?

I know i want to use an update statement but im having trouble with the structure of the query
The first of the following 2 SQL statements will create the new column in the table, and the second, update statement will populate the new column from the old column.
alter table Table1 add newColumn char(32);
update table1 set newColumn=oldColumn;
commit;
You can't create a new column using an update, you have to do that first. Then it's just as simple as:
update TheTable set NewColumn = OldColumn
UPDATE table
SET column2 = column1
update table_name set column_to_be_changed = existing column

update conditionally a field in case parameter is not null

I need a query to update one field. If the passed parameter is null the do NOT update it with the null value of the parameter
update myTable
set myField1 = :param1
environment: hibernate and oracle
Can't you just put it in there where clause?
update myTable
set myField1 = :param1
where :param1 is not null
That'll avoid extra DML. Alternatively you can do:
update myTable
set myField1 = decode(:param1, null, myField1, :param)
But that means you'll update a field to the same, which isn't really optimal when you don't have to.