Im new in all of this and Im having a dificult time doing the following:
After an Alter Table where Im adding a new column Im updating the values based on another column. What a want to do is where there is a null after the process put the name "LIV".
How would you do it?
Thanks in advance
Code:
ALTER TABLE TEST1 ADD COLUMN UBIC2 STRING;
UPDATE TEST1
SET UBIC2 = UBIC
WHERE ZONA ="X1" or ZONA ="X2"
Tried to use an IFNULL(UBIC2, "LIV")
You could update the table with a case expression. Also, since you want to update all the rows of the table, you can lose the where clause:
UPDATE test1
SET ubic2 = CASE WHEN zona IN ('X1', 'X2') THEN ubic ELSE 'LIV' END
Tried this ?
UPDATE test1
SET ubic2 = CASE WHEN zona IN ('X1', 'X2') THEN ubic ELSE 'LIV' END
where 1=1
Related
I am running SQL Server and am needing to update hundreds of entries. Column A has unique values and based on that I need to update column B of the same table, all with different values. Essentially I am looking for a way to perform the following but in a bulk manner:
UPDATE table
set column B = 'value'
where column A = 'unique value'
Is this what you are expecting,
UPDATE table
set column B = case when column A = 5 then 'unique string' end;
How about:
update table set
columnB = concat('unique ', columnA)
you may, or may not, need a where clause, depending on your needs.
With hundreds of values you better have the {unique value - update value} mapping defined in a separate table as TT is asking. But otherwise, if you are okay with hard-coding those mappings in your UPDATE statement:
UPDATE t
SET B = CASE A
WHEN 'unique value1' THEN 'value1'
WHEN 'unique value2' THEN 'value2'
WHEN 'unique value3' THEN 'value3'
ELSE 'Unknown'
END
FROM table t
BTW, the above is only for 3 mappings
Here, you need to write cursor to update a column value in the same table
BEGIN
DECLARE c_a, c_b TEXT;
DECLARE c_ab CURSOR FOR
SELECT column_a,column_b FROM c_table;
OPEN c_ab;
LOOP
FETCH c_ab into c_ca, c_cb;
IF c_ca = "0" THEN
update c_table SET column_b = "No" WHERE
column_a = c_ca;
END IF;
IF c_ca = "1" THEN
update c_table SET column_b = "Yes" WHERE
column_a = c_ca;
END IF;
END LOOP;
CLOSE c_ab;
END
Working and tested code [please refer some cursor tutorials to update according to your condition] and with this you can update table in bulk and speedy
Thanks #Jim Macaulay. That did it. Thank you everyone else for your input.
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
I added new column to my existing table, my aim to insert data to the new column with using existing column's data after some manipulation, such as adding prefix. the new and existing column's type is string.
How can I perform it ?
I have no idea about plsql. SQL is enough for this situation?
I haven't got PL/SQL here, so I can't test it but it should be easy.
Let's start by altering the table:
ALTER TABLE table_name
ADD new_column varchar2(50);
Then, let's update it.
We'll start by adding all the values from the old_column that don't begin with the prefix.
UPDATE table_name t1
SET t1.new_column = 'prefix' || t1.old_column
WHERE t1.old_column NOT LIKE 'prefix%';
Then, we can simply copy the values from the old_column that already have the prefix
UPDATE table_name t1
SET t1.new_column = t1.old_column
WHERE t1.old_column LIKE 'prefix%';
Just update all records in a table.
It's possible to do it in single run through all records:
update existing_table
set
newcolumn = case
when (length(oldcolumn) = 13) and (oldcolumn like '+%')
then oldcolumn
when (length(oldcolumn) = 12) and (oldcolumn like '90%')
then '+' || oldcolumn
when (length(oldcolumn) = 10) and not (oldcolumn like '+%')
then '+90' || oldcolumn
else '?'
end
After update it's possible to check for invalid conversions:
select oldcolumn, newcolumn
from existing_table
where newcolumn = '?'
and correct case conditions or update remaining records one by one.
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
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'