Changing datatype of a column in database - sql

I have to change the datatype of a column from CHAR to INTEGER in database. I used the following query:
ALTER TABLE cert_request
ALTER column CERT_REQUEST_NBR SET TYPE(INTEGER);
however it's showing error in the keyword "column" and giving the error code as -104 on execution. Following is the error log;
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=ALTER TABLE CERT_REQUEST ALTER CERT_R;BEGIN-OF-STATEMENT;<create_proc>, DRIVER=3.50.152
SQLState: 42601
ErrorCode: -104
I also tried the using the Modify keyword, but it isn't identified in my database.
I also tried the following query but again of no use.
alter table CERT_REQUEST alter column cert_request_nbr integer
It gives the following error
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=integer;umn cert_request_nbr;<alter_col_action1>, DRIVER=3.50.152
SQLState: 42601
ErrorCode: -104
Please provide me some suggestions.

Use MySQL Type Conversion or ALTER
ALTER TABLE cert_request MODIFY CERT_REQUEST_NBR INTEGER;
Or try MySQL Type Conversion
CAST(CERT_REQUEST_NBR AS UNSIGNED)

You have to use MODIFY not ALTER in MYSQL
ALTER TABLE cert_request
MODIFY CERT_REQUEST_NBR INTEGER;
And in DB2 you can try like this:
ALTER TABLE cert_request ADD COLUMN Temporary INTEGER;
UPDATE cert_request SET Temporary=CAST(CERT_REQUEST_NBR AS INTEGER);
ALTER TABLE cert_request DROP COLUMN CERT_REQUEST_NBR;
ALTER TABLE cert_request CHANGE Temporary CERT_REQUEST_NBR INTEGER;

Have you tried:
ALTER TABLE `cert_request` CHANGE `CERT_REQUEST_NBR` `CERT_REQUEST_NBR` INT(10) NOT NULL;
Be careful any data that is not a integer will be lost.

It will be helpful if you specify which database you are using . In oracle the solution should be
Alter table cert_request modify CERT_REQUEST_NBR number(30)
In db2(based on the error you provided, I believe your database is db2) the solution is
Alter table cert_request alter column CERT_REQUEST_NBR set datatype decimal(30)

You are missing the word 'Data' between 'Set' and 'Type'.
It should be like this:
ALTER TABLE cert_request
ALTER column CERT_REQUEST_NBR SET DATA TYPE(INTEGER);

May be, It happen due to varchar data present in field values.
Delete that data, and then run your query

It is because your column is having data which are not integer. You will either need to remove those data and try or you can run a query mentioned in above post which will make your data null which are not int.

Related

How to copy SQL column data to new column then drop the original column?

I am following the recommendation in this sql snowflake forum in order to transform an integer data column into a varchar by creating a new column. I want to drop the original integer column when I am done, but doing so always results in the new column no longer working and any future queries erroring out.
For instance, I have test_num is the integer and test_num_to_char is the varchar
alter table test_table
add test_num_to_char varchar as CAST(test_num as varchar)
then
alter table test_table
drop column test_num
select *
from test_table
results in an error message:
SQL execution internal error: Processing aborted due to error 300002:224117369
Is there a different transformation method that removes the dependency on the original integer column so I can drop it?
alter table test_table add test_num_to_char varchar(10);
go
update test_table set test_num_to_char = CAST(recno as varchar);
Try the TO_DECIMAL transformation method.
It's documentation is given here

User defined type dropped while in use

I have an Oracle database with a table as follows:
create table Table1
(Column1 number(5,0),
Column2 special_type);
Now, due to some data errors, the support team decided the fix would be to drop and recreate the Type.
I now have a table as follows:
Table1
Column1 number(5,0),
Column2 null
The problem is, I cannot drop the table, I get a "Table has errors" message. I cannot alter the table, I get a "Table has errors" message. I have tried to manipulate the DDL in Oracle SQL Developer, guess what I get? A "Table has errors" message.
Can anyone point me in the right direction?
It seems support team have dropped the type forcefully. In normal scenario, you cannot drop a type if it has any dependent table.
See demo:
SQL> CREATE OR REPLACE TYPE NUU AS TABLE OF NUMBER;
/
Type created.
SQL> CREATE TABLE TBLLL(NUM NUU)
NESTED TABLE NUM STORE AS VVVVV ;
/
Table created.
Now when i try to do a simple drop type, i get the below error:
SQL> drop type nuu;
drop type nuu
*
ERROR at line 1:
ORA-02303: cannot drop or replace a type with type or table dependents
So i drop it forcefully:
SQL> drop type nuu force;
Type dropped.
And when i try to make a select i get the error:
SQL> select * from tblll;
select * from tblll
*
ERROR at line 1:
ORA-04063: table "USER.TBLLL" has errors
So in order to Alter the table you first need to create the type back. Once the type is created back, your table definition becomes correct and then you can Alter your table.
Other solution is to drop the table and recreate it which you already mentioned is not working.
You can try the following -
re-create the type
create another table with same table structure
insert the data into the second table
use the second table now.
You may want to not recreate the type since it caused issues earlier. Find an alternative. It might solve your issue.

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

Add a column SQL query in Oracle database

I am using Oracle Database (version is 9i) and I want to add a column to a current table in oracle database.
I want to add an integer column to keep track of invalid tries per user, so default value should be 5.
When I try to execute this query in Sql*Plus it gives an error table or view doesn't exist ( I have double checked table name is correct.
ALTER TABLE CustApps_user ADD VALID_TRIES INT DEFAULT 5 NOT NULL;
I guess the error you're getting is ORA-00942. This can mean a number of things, but basically it means the object does not exist in the current scope and context of what you're doing. So for instance it is the error thrown when we attempt to build a view on a table in another schema when we have been granted privileges through a role and not directly.
In your case it probably mean that the table is in another schema. You normally may be accessing it through a view or synonym. You can easily check this by querying the data dictionary:
select owner, object_type
from all_objects
where object_name = 'CUSTAPPS_USER'
alter table
table_name
add
(
column1_name column1_datatype column1_constraint,
column2_name column2_datatype column2_constraint,
column3_name column3_datatype column3_constraint
);
Here are some examples of Oracle "alter table" syntax to add data columns.
alter table
cust_table
add
cust_sex varchar2(1) NOT NULL;
Here is an example of Oracle "alter table" syntax to add multiple data columns.
ALTER TABLE
cust_table
ADD
(
cust_sex char(1) NOT NULL,
cust_credit_rating number
);
You have to add bracket in query:
ALTER TABLE CustApps_user ADD (VALID_TRIES INT DEFAULT 5 NOT NULL);
INT is legal, but it will be converted to NUMBER, so you can also use:
ALTER TABLE CustApps_user ADD (VALID_TRIES NUMBER(38,0) DEFAULT 5 NOT NULL);
or change (decrease) NUMBER precision.

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 )