How to change datatype of the column in derby database? - sql

Iam trying to change the datatype of the column from integer(9) to Numeric(14,3).
but not able to change
i tried below query
alter table TableName alter ColumnName NUMERIC (14,3);

hope below one will help.
ALTER TABLE Table_Name ALTER COLUMN Column_Name SET DATA TYPE NUMERIC(14,3);

Related

Alter column length in SQL

ALTER TABLE tableName
MODIFY COLUMN columnName VARCHAR (256);
Error :
Incorrect syntax near 'VARCHAR'
The syntax for modifying a column definition is depending on used database. For Microsoft SQL Server for example, you would need to replace MODIFY COLUMN with ALTER COLUMN.
For better help you should specify your database type.

alter data type of existing column from bigint to varchar in Apache derby

I am using Apache derby database v 10.9.1.0. There is one existing table Country-having column LawID of type bigint. It contains records having integer data only. Due to some business reason, I need to alter its data type from 'bigint' to 'varchar' . I tried following two ways to alter existing table. But both ways did not work.
a. first way
ALTER TABLE Country ADD COLUMN LawID_NEW VARCHAR(50);
UPDATE Country SET LawID_NEW = LawID;
ALTER TABLE Country DROP COLUMN LawID;
RENAME COLUMN Country.LawID_NEW TO LawID;
It shows message like :Columns of type 'VARCHAR' cannot hold values of type 'BIGINT'.
b. second way
ALTER TABLE Country ALTER LawID SET DATA TYPE VARCHAR(50);
It shows error message like : Invalid type specified for column 'LawID'. The type of a column may not be changed.
Any help related to correct alter query is highly appreciated, Thanks
I think the first method would work with this change:
UPDATE Country SET LawID_NEW = TRIM(CHAR(LawID));
ALTER TABLE tablename MODIFY columnname VARCHAR(20);
This works in mysql. Give it a try.
Or
ALTER TABLE table CHANGE columnname columnname VARCHAR(20);

Convert varchar to date in sql loader

I have loaded the date field with dates and type is varchar.
How to convert date field(varchar) to date field(date) in oracle express/sql loader while displaying the fields?
You can't change the data type of a column in a permanent table from VARCHAR2 to DATE when it has data.
You can, however, add a new column
ALTER TABLE table_name
ADD( new_date_column DATE );
move the data over
UPDATE table_name
SET new_date_column = to_date( old_varchar2_column, format_mask );
drop the old column
ALTER TABLE table_name
DROP COLUMN old_varchar2_column;
and then rename the new column to the old column name
ALTER TABLE table_name
RENAME COLUMN new_date_column TO old_column_name
Of course, once you do this, you'll need to change your SQL*Loader script to convert the data to a DATE if you ever want to load into this table again.

Alter every double fields of a table

I need to change every double precision field of a table to numeric(15,3) type,
how can i do this job quickly with a stored procedure that iterate through the field of a given table and if the type is double precision alter column to numeric ?
following query should return query to update these tables... you can add table filter if you want. Copy the result and run it.
select 'ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name + 'TYPE numeric(15,3)'
from information_schema.columns
where data_type = 'double precision'
and table_name = 'YOUR_TABLE_NAME'
TEST IT BEFORE RUN IT. I DID NOT TEST THIS YET.
While another question will do it for all columns; a simple "alter table [tablename] alter column [columnToAlter] type numeric(15,3). You shouldn't need to run them through a cursor; any value that's not going to be affected by this should remain unaffected.
If you can't do it by changing the datatype itself, a simple update [tablename] set [columnname] = cast(columnname as numeric(15,3) should also work.
Hope that helps!

How to convert column type from varchar to date in PostgreSQL?

I have varchar data type column and date data type column.
I have to update varchar column data into date column in PostgreSQL.
Is it possible?
Thanks.
ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE DATE
using to_date(<columnname>, 'YYYY-MM-DD');
UPDATE tableName SET dateColumn=to_date(varcharColumn, 'DD MM YYYY')
Assuming you are saving "07 04 2010"
You can find further examples and explanation in the documentation:
http://www.postgresql.org/docs/current/interactive/functions-formatting.html
to_date('05 Dec 2000', 'DD Mon YYYY')
syntax for typecasting:
alter table table_name alter column_name
type converting_data_type using(column_name::converting_data_type)
converting from varchar to date
alter table table_name
alter column_name type date using(column_name::date)
To convert column type from timestamp to date in postgresql with explicit typecast:
Explicit typecast allows our existing data to be converted to our new type when the column type is updated.
There is slight change in syntax for explicit typecast for keyword USING
Syntax:
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type
USING expression;
eg. Suppose we have table user_data with a column date_of_birth which took timestamp earlier, but now we want it to store only date.
Query:
ALTER TABLE user_data
ALTER COLUMN date_of_birth TYPE date
USING date_of_birth::date;