Alter table add column with data computed from the same table - sql-server-2012

I have a table with column "datetime2_asstring". I would like to add a column "datetime2_asdatetime2" to that table.
For this I execute queries:
1) add column datetime2_asdatetime2
2) update column datetime2_asdatetime2 with values from cast(datetime_asstring TO datetime2)
I wonder if there is SQL Syntax to add a column and at the same time set computed value for every row on this column in the table, all in one query (ALTER TABLE) in SQL Server?

The code below would work for the versions that you have specified:
ALTER TABLE datetime2_asstring
ADD datetime2_asdatetime2 AS CAST(datetime_asstring AS datetime2)

Related

I want to add two columns names to an existing table in Impala Query

I am writing the following query to add a column at a specified position but getting the below error:
alter table quantum_raw_dev.rpt_backup_allocation
change upt_type upt_type STRING after tray_size;
You can add one or more columns to the end of the column list using:
ALTER TABLE <table_name> ADD COLUMNS (col_name col_type, ...);
[note: there is NO comma between column name and type]
Adding or Removing Columns
You can add one or more columns to the end of the column list using ADD COLUMNS,
or (with Impala only) you can delete columns using DROP COLUMN.
The general syntax is
ALTER TABLE tablename ADD COLUMNS (col1 TYPE1,col2 TYPE2,… );
ALTER TABLE tablename DROP COLUMN colname;
For example, you can add a bonus integer column to the employees table:
ALTER TABLE employees ADD COLUMNS (bonus INT);
Or you can drop the office_id column from the employees table:
ALTER TABLE employees DROP COLUMN office_id;
Notes
DROP COLUMN is not available in Hive, only in Impala. However, see “Replacing All Columns” below.
You can only drop one column at a time.
To drop multiple columns, use multiple statements or use the method to replace columns (see below).
You cannot add a column in the middle of the list rather than at the end.
You can, however, add the column then change the order (see above) or use the method to replace columns (see below).
As with changing the column order, these do not change the data files.
If the table definition agrees with the data files before you drop any column other than the last one,
you will need to recreate the data files without the dropped column's values.
If you drop the last column, the data will still exist but it will be ignored when a query is issued.
If you add columns for which no data exists, those columns will be NULL in each row.
Replacing All Columns
You can also completely replace all the columns with a new column list.
This is helpful for dropping multiple columns,
<h1>or if you need to add columns in the middle of the list<h1>
<h2>(like your use case)<h2>
The general syntax is
ALTER TABLE tablename REPLACE COLUMNS (col1 TYPE1,col2 TYPE2,… );
This completely removes the existing list of columns and replaces it with the new list.
Only the columns you specify in the ALTER TABLE statement will exist, and they will be in the order you provide.
Note
Again, this does not change the data files, only the metadata for the table,
so you'll either want the new list to match the data files or need to recreate the data files to match the new list.
I do not think you can add columns in between columns in Impala like above.
You can backup the data, drop the and recreate with new structure, and load the table from backup. Also if you have HIVE in your system you can try to do below steps -
Add column first and then use below commands to move columns around.
ALTER TABLE tab ADD COLUMNS (id BIGINT);
This moves id column to the beginning.
ALTER TABLE tab CHANGE COLUMN id id BIGINT first;
This moves existing_col after id.
ALTER TABLE tab CHANGE COLUMN existing_col existing_col string AFTER id;
Please refresh/invalidate metadata after applying all DDLs.
You cannot add column in between. Best way is to archive the data in another table. Drop the impala old table and create a fresh table with new columns as per the desired location and then reinsert the data.

SQL Server Add Default datetime column for existing table

I want to have a new column in the table that will show the date and time of the inserts, but without modifying the queries to include the column itself.
I have added the new column in the following way:
ALTER TABLE DBO.HOURLYMODULETIMES
ADD CreateTime datetime DEFAULT NOT NULL getdate()
This adds the values to previous entries, but when I try to INSERT INTO the table without including the new column
INSERT INTO DBO.HOURLYMODULETIMES VAlUES
(99999999,11111,2222,'JA')
Table has 5 columns ID, AVGMODULETIME, SUMHOURS, USERNAME, CreateTime(newly added). I get the following error:
Column name or number of supplied values does not match table definition.
Is it possible to create such a column without modifying the queries?
You have to specify the columns now when you want to omit one of them when doing INSERT:
INSERT INTO DBO.HOURLYMODULETIMES (ID, AVGMODULETIME, SUMHOURS, USERNAME)
VALUES (99999999,11111,2222,'TEST')
It's good programming practice to always do this, since table definitions may change over time - as you have noticed!

Alter column of type timestamp

I want to update an empty table , which has a column of type timestamp to varbinary(8)
I used the following command
ALTER TABLE Notification ALTER COLUMN RowRevisionID varbinary(8)
and I get and an error
Cannot alter column 'RowRevisionID' because it is 'timestamp'.
How can I change a timestamp column type?
I do not wish to drop the column an add a new one , because that will create a column at the end , and I wish to preserve column order to use this table in an INSERT INTO
You unfortunately cannot make a change to a timestamp column, as the error implies; you are stuck with what you have. Also, each table can only have one timestamp column, so you cannot duplicate the column in any solution.
Your best bet (depending on the size of the table) might be to copy the data into a staging table (using SELECT * INTO MyTempTable FROM OriginalTable syntax to preserve the timestamp values), then drop and recreate the original table with the desired columns in the desired order and reinsert the data, or you could add a new VARBINARY(8) column to the existing table and drop the timestamp column, preserving the original table. There are probably other solutions along the same lines as these, but all will require a new column, rather than an ALTER COLUMN script.
You are looking for:
ALTER TABLE Notification DROP RowRevisionID;
and
ALTER TABLE Notification ADD RowRevisionID varbinary(8) AFTER myOtherColumn;

populate a column with a value based on value of other column alter table sql

I have a table 'Data' with a column 'Date'
I need to add another column called flag and populate it with 0 if the date is less than 2 years from current date and populate it with 1 if the date is more than 2 years from current date.
I did it by adding column using alter table and using update set statement as below
alter table data add flag INTEGER constraint flag_value check (flag in(0,1));
Is there a way to do this using just one alter table statement without using update set?
Is there a way to do this using just one alter table statement without using update set?
Not in sqlite, which does not support computed columns and there is no way to run a default UPDATE via an ALTER TABLE statement.
You could use the view approach from the linked question, or do as you've done and issue separate ALTER TABLE and UPDATE statements.

Add a column to a DB2/400 table with a specific ordinal position

Is there an SQL command on the AS400/iSeries/System-i/whatever to add a column to a table in a specific ordinal position, or moving an existing column to a different position?
IBM i 7.1 now allows you to add a column in front of another.
ALTER TABLE table ADD COLUMN colname ... BEFORE othercolumn
No. The ALTER TABLE statement will allow you add a column to a table, but, according to the documentation:
The new column is the last column of the table; that is, if initially there are n columns, the added column is column n+1.
If you'd like to change the order of columns in your table, your best bet is to:
Use the RENAME statement to rename the table.
Recreate the table, with its original name, with the columns in the order that you want.
Use an INSERT SELECT to populate the new table with the data from the renamed table.
When you're sure the data is intact, you can drop the renamed version of the table.