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

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

Related

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

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)

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

updating a field where field cannot be less than zero

im using the following query to update my field
UPDATE tableName SET fieldName = fieldName-10
WHERE id=1;
and this is working fine in my case but i want that if the result is less than zero than query should not execute.
i've tried this
UPDATE tableName SET fieldname = fieldName-10
WHERE id=1 and fieldName>0;
but it returns with an error syntax error in your UPDATE statement.
thanx in advance
did you mean this? if pqty is the field of your table.
UPDATE tableName SET fieldname = pqty-10 WHERE id=1 and pqty>10;

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.

Fill entire SQL table column

I just added a new column in my DB which I need to propagate with a specific text value (Confirmed). Is there a simple way to apply to all my data entries so I don't have to go through all my rows and type the value in?
Thanks
you run the statement:
UPDATE whateveryourtableis SET whateveryourcolumnis = 'whatever';
Yould could make the desired value the new column's DEFAULT e.g.
ALTER TABLE MyTable ADD
my_new_column VARCHAR(20) DEFAULT 'Confirmed' NOT NULL;
Yes there is:
UPDATE [table]
SET [column] = 'Confirmed'