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

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

Related

update and concatenate columns in PostgreSQL

I want to update and concatenate 3 columns in a new a attribute I've already created in my table. However when I execute my code, it puts attributes that before had values in null.
My code example:
update saber2012_1
set estu_fecha_nacimiento = Concat(estu_nacimiento_dia,'/',estu_nacimiento_mes,'/',estu_nacimiento_anno);
In this picture all values that is in null before these had values.
when I make this update
ALTER TABLE saber2012_uno ADD COLUMN punt_c_naturales int;
update saber2012_uno set punt_c_naturales = round((PUNT_BIOLOGIA::numeric + PUNT_FISICA::numeric + PUNT_QUIMICA::numeric) / 3,0);
alter table saber2012_uno DROP COLUMN PUNT_BIOLOGIA;
alter table saber2012_uno DROP COLUMN PUNT_FISICA;
alter table saber2012_uno DROP COLUMN PUNT_QUIMICA;
all is well. but I do not know because previous update is incorrect.
Perhaps you want concat_ws():
update saber2012_1
set estu_fecha_nacimiento = concat_ws('/', estu_nacimiento_dia, estu_nacimiento_mes, estu_nacimiento_anno);
This will ignore any of the arguments that are NULL.
However, your update will not change any other columns in the table. Perhaps the results are just coming back in a different order, when you query after the update.

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

SQL Trigger update another table

I am newbie to triggers... can anybody help me with a trigger?
I have Table:
Name | Number
I want to write a trigger when my table receives a query like
update MyTable
set Number = Number + 1
where Name = 'myname'
When this query is running, the trigger should update another table for example:
Update MyTable 2
set Column = 'something'
where Name = 'myname (above name)
Thank you very much !
You will need to write an UPDATE trigger on table 1, to update table 2 accordingly.
Be aware: triggers in SQL Server are not called once per row that gets updated - they're called once per statement, and the internal "pseudo" tables Inserted and Deleted will contain multiple rows, so you need to take that into account when writing your trigger.
In your case, I'd write something like:
-- UPDATE trigger on "dbo.Table1"
CREATE TRIGGER Table1Updated
ON dbo.table1 FOR UPDATE
AS
BEGIN
-- update table2, using the same rows as were updated in table1
UPDATE t2
SET t2.Column = 'something'
FROM dbo.Table2 t2
INNER JOIN Inserted i ON t2.ID = i.ID
END
GO
The trick is to use the Inserted pseudo table (which contains the new values after the UPDATE - it has the exact same structure as your table the trigger is written for - here dbo.Table1) in a set-based fashion - join that to your dbo.Table2 on some column that they have in common (an ID or something).
create a trigger on table 1 for update:
CREATE TRIGGER dbo.update_trigger
ON table1
AFTER UPDATE
AS
BEGIN
DECLARE #Name VARCHAR(50)
SELECT #Name=Name FROM INSERTED
Update MyTable 2
SET Column = 'something'
WHERE Name = #Name
END
GO
try this ;)

PL-SQL manipulate column and insert

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.

How to insert one column into other column within the same table in SQL Server

I need to insert one column's data into another column within the same table.
Can anybody tell me how to write this?
Thanks
UPDATE table
SET col_2 = col_1
If you want to copy data from one column to another on the same table:
UPDATE table_name SET
destination_column_name=orig_column_name
WHERE condition_if_necessary
IF you want to add a new column and copy the original data to that column:
ALTER TABLE table_name
ADD new_column_name column_type NULL
UPDATE table_name SET
destination_column_name=orig_column_name
WHERE condition_if_necessary
If you want the column to be non-nullable, then you can set it to a default value before doing the update.
begin transaction
alter table Song add SortArtist nvarchar(128) not null default N''
go
update Song set SortArtist = Artist
commit transaction
alter table [dbo].[GetPermission]
add username1 varchar(100) ----------------ading new column username1
update GetPermission set username1=username