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

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

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, 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

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'