How to change column data type of metadata table . What will be the update statement be like? - ssms

enter image description here
I need to change data type from int to bigint. This has to be changed in metadata as well after changing the source table . Please someone get back to me fast .

You can use the Alter Command.
ALTER TABLE tableNAme ALTER COLUMN columnName bigint;
GO
This will automatically change your metadata table as well. you don't need to change it yourself

Related

Create a new column based on other columns

I've managed to create the following column in a new table:
CREATE TABLE t_issue_dates as
SELECT issue_d,
cast(substr(issue_d,5,4) as numeric) as issue_year
FROM myDB
(The code recodes a year-month variable to a year-only variable)
However, i can't add this variable to my existing table "myDB". I've tried using the:
ALTER TABLE myDB ADD v_year - command, but i can't manage to get it right.
Does anyone have an idea how i add the above variable to the "original" table myDB?
Thank you!
First, many databases support computed or generated columns. That means that you can add a virtual column to the database by doing:
alter table t_issue_dates add issue_year as (cast(substr(issue_d, 5, 4) as numeric));
I recommend this approach because issue_year is always up-to-date.
Second, you can do this as an actual column, but the value can get out-of-date and needs to be re-calculated for each inserted/updated row:
alter table t_issue_dates add issue_year numeric;
update t_issue_dates
set issue_year = cast(substr(issue_d, 5, 4) as numeric);
For the record, I would use int rather than numeric.
Assuming you are using MSSQL. It would be worth of reading documentation or a simple google on how to insert
Adding column :
Alter table t_issue_dates
Add V_year Int
Next step: This will only insert data for this particular column.
Insert into t_issue_dates (v_year)
SELECT
cast(substr(issue_d,5,4) as numeric)
FROM myDB

False error message for binary data truncation(Msg 8152)

I am getting false error message while data inserting to some table.
The data type for the field is Varchar(20) and the data being inserted has max
data length is 6.
I don't understand where's the issue is. Although I can avoid this by adding
SET ANSI_WARNINGS OFF
But it'd be workaround not a solution:
It seems your column SchemaName maxlength property smaller then value which you going to insert.
Update your column length as per your data.
ALTER TABLE Temp ALTER COLUMN SchemaName VARCHAR(XXXX)
I just found the root cause of this issue.
Actually I was trying to insert sysname data type value into varchar().
So must specify proper length to hold sysname data type.
Your target table is not sufficient to hold the data that you are trying to insert. It seems your target size would be varchar(256) but you have given varchar(10). Just extend the column size to resolve this issue.
Run this DDL
Alter table temp alter column SchemaName varchar(256)
This error come when your column has less size them.
Update size of your column.

ERROR: syntax error at or near "modify" - in postgres

I executed this SQL statement in Postgres
alter table user modify column 'distinguishedName1' text;
and
alter table user modify column distinguishedName1 text;
user is the table name
distinguishedName1 is the column name with integer data type.
I wanted to modify the data type to boolean or text or varchar(256) etc based on user's input. But when I run the query I get the error
ERROR: syntax error at or near "modify"
Not sure what is the problem. Help required on right query.
POSTGRES syntax for altering column type :
ALTER TABLE user ALTER COLUMN distinguishedName1 TYPE text;
Try this:
ALTER TABLE "user" ALTER COLUMN distinguishedName1 TYPE text USING code::text;
or
ALTER TABLE "user" ALTER COLUMN distinguishedName1 TYPE text
Also do note that the USING is optional. See the manual here:
The optional USING clause specifies how to compute the new column
value from the old; if omitted, the default conversion is the same as
an assignment cast from old data type to new. A USING clause must be
provided if there is no implicit or assignment cast from old to new
type.
On a side note try to avoid naming your tables as reserved keywords.
alter table user Alter column distinguishedName1 text;
Syntax mistake , for sql server you have to use alter to modify the column of table

How to change column varchar to clob in oracle

I have a column details designed as varchar in oracle DB, this DB is being used now for customers and some rows already have data stored.
Now I want to change the column details to a Clob column. What is a smart way to accomplish this?
(as the previous answer) and here's the code:
ALTER TABLE atable
ADD (tmpdetails CLOB);
UPDATE atable SET tmpdetails=details;
COMMIT;
ALTER TABLE atable DROP COLUMN details;
ALTER TABLE atable
RENAME COLUMN tmpdetails TO details;
Add a clob column to the table
update clob column with values from varchar column
drop varchar column
rename clob column to varchar columns name
But this will not maintain the position of your column. It will move your column to the end of table. So if you want to maintain the position of your column as well follow these steps.
alter table atable add (tempdetails varchar2(4000));
update atable set tempdetails = details;
update atable set details = null; -- this is necessary to change data type
alter table atable modify details long; -- this is required because you can not change directly to clob.
alter table atable modify details clob;
update atable set details=tempdetails;
alter table atable drop column tempdetails;
This is the way in which you will maintain the data and position of your column intact even after changing the datatype. For detail information with example see here : http://www.oraclebin.com/2012/12/how-to-change-varchar2-to-clob-datatype.html
if you need your table data to be accessible during the process.. look at
DBMS_REDEFINITION
see a similar question on asktom
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1770086700346491686

How to alter a column datatype for derby database?

I am trying to alter a datatype for a derby db column. The current price column is set as DECIMAL(5,0). I would like to alter it to DECIMAL(7,2). I did this :
alter table item alter column price set data type DECIMAL(7,2);
But it did not work, and showing the error:
Error: Only columns of type VARCHAR may have their length altered.
May I know how is it possible to alter it? Thank you.
Here is the Derby SQL script to change column MY_TABLE.MY_COLUMN from BLOB(255) to BLOB(2147483647):
ALTER TABLE MY_TABLE ADD COLUMN NEW_COLUMN BLOB(2147483647);
UPDATE MY_TABLE SET NEW_COLUMN=MY_COLUMN;
ALTER TABLE MY_TABLE DROP COLUMN MY_COLUMN;
RENAME COLUMN MY_TABLE.NEW_COLUMN TO MY_COLUMN;
I think you can do like this:
ALTER TABLE SCHEMA.TABLE ALTER "COLUMN-NAME" SET DATA TYPE VARCHAR(255);
(column-Name SET DATA TYPE VARCHAR(integer)) for Datatype String as an example...
Here's a slightly more complicated way to alter the column's data type in this fashion:
Add a new column, of the desired data type
Issue "update ... set new-column = old-column to copy the data from the old column to the new column
drop the old column
Rename the new column to have the name of the old column.
Slightly more steps, but in the end the effect will be the same.
If you have trouble working out the exact details of the SQL to do this, let us know and we'll help.
You can alter table like this:
ALTER TABLE [table] ALTER COLUMN [column] SET DATA TYPE [type];
Or in Rails, just use:
change_column :table_name, :column_name, :integer
Posgtes Solution :
ALTER TABLE prices_table ALTER price_column TYPE decimal (7,2 )