Redshift won't change column type - sql

I'm trying to change a column in Redshift from varchar to integer. I've already checked and the strings are all numbers so it should force fine.
When I run:
alter table schema.table_name alter column "id" type int;
I get the following error:
ERROR: target data type "int8" is not supported [SQL State=0A000]
I've checked the Redshift documentation and just to rule out a few potentials:
The field is not a primary or foreign key
There's no compression encodings on it
There's no default values
The code is not in a transaction block
Any pointers would be amazing, thank you!

Alter column type is for varchar types - "ALTER COLUMN column_name TYPE new_data_type --
A clause that changes the size of a column defined as a VARCHAR data type." See: https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html

Related

Changing the datatype with a custom type in postgres custom data type

I have a custom type in Postgres DB called money_with_currency Created as:
CREATE TYPE public.money_with_currency AS (currency_code char(3), amount numeric);
We want to change the type of currency_code from char(3) to varchar.
I thought the code would be something like:
ALTER TYPE public.money_with_currency ALTER ATTRIBUTE currency_code SET DATA TYPE varchar;
But got an error:
ALTER TYPE public.money_with_currency ALTER ATTRIBUTE currency_code SET DATA TYPE varchar;\n"
** (Postgrex.Error) ERROR 0A000 (feature_not_supported) cannot alter type "money_with_currency" because column "prog_fees.amount" uses it
Any thoughts if there is a solution without having to do manual migration to all columns using the type?
First create another custom type and use an alter table query to change the column type of the prog_fees. Then alter the type of public.money_with_currency. Then again execute a alter query to table using public.money_with_currency type. Then it will work
You will need to create a new type with the desired structure. But as there is no direct cast from the old to the new type, you need to use a row constructor when altering the type of the existing column:
CREATE TYPE money_with_currency_new AS (currency_code text, amount numeric);
alter table prog_fees
alter column amount type money_with_currency_new
using ((amount).currency_code, (amount).amount)::money_with_currency_new;
Note the parentheses around the column name when referencing the type's attributes. They are required.
After that you can drop the old type and rename the new type to the old name:
drop type money_with_currency;
alter type money_with_currency_new
rename to money_with_currency;

Cockroachdb varchar column length isn't flexible

I have a spring boot application that connects to Cockroachdb. I have the following script in my flyway using which the table gets created:
CREATE TABLE IF NOT EXISTS sample_table (
name varchar,
groups varchar,
PRIMARY KEY (name));
The application starts fine, but whenever there is a value for the 'groups' column that is greater than 255 length, I get an error :
Caused by: org.postgresql.util.PSQLException: ERROR: value too long for type VARCHAR(255)
In the sql script, I have mentioned the column 'groups' as 'varchar' which should not restrict the length so I am not sure why am I getting this error.
There isn't an implicit default limit on varchar in CockroachDB. This error indicates that the groups column was initialized with the type varchar(255) when the table was created. Running SHOW CREATE TABLE sample_table; should confirm this.
It's possible that something unexpected is going on in the flyway and the table is not being created how you want it to be created.

varchar column returning exponential representation of number

I have a varchar(max) column that sometimes returns me the exponential format of the number like so:
1e+006
How do I alter the column in the table to always have it show me the actual number and not an exponential representation of the number?
I would alter the column to a different data type or something, but I have no idea what I'd alter it to, since I don't understand why this is happening in a varchar column.
A varchar like '1e+006' can be converted to float, which can then be further converted to any numeric type you like.
So this might be as simple as
alter table mytable alter column mycolumn float
perhaps followed by a conversion to a decimal type:
alter table mytable alter column mycolumn decimal(20,5)

Can't ALTER COLUMN TYPE in PostgreSQL "0A000: Cannot alter column "FIELDA" of relation "TABLEA"

Trying to run the below script:
ALTER TABLE SCHEMA.TABLEA
ALTER COLUMN FIELDA TYPE VARCHAR(5)
And I am getting the following error:
0A000: Cannot alter column "FIELDA" of relation "TABLE"
The field is currently an integer and contains no data, only nulls.
As per the AWS documentation you cannot change the data type of a VARCHAR only the size.
https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html

constraint check range float

I've got a problem with my sql code (working with oracle).
I'm triyng to make a constraint with body height, which should be between 1,0 and 2,4. The data type is a float. The decimal place is divided with a comma.
This ist the code:
alter table tableName
add constraint check_height
check (columnName between 1,0 and 2,4);
I tried to divide the decimal places in the code (I can't change the decimal place holder in the data list) with comma and points, I also tried to change the range and to show the range with < >. Nothing worked so far. Has anyone an idea what I am missing?
The error message is: 'check constraint violated'.
cheers
If you already have data in your table with values that would violate the constraint, then you can't create it with the default validate clause:
create table tableName(columnName number);
insert into tablename (columnName) values(2.5);
alter table tableName
add constraint check_height
check (columnName between 1.0 and 2.4);
SQL Error: ORA-02293: cannot validate (STACKOVERFLOW.CHECK_HEIGHT) - check constraint violated
02293. 00000 - "cannot validate (%s.%s) - check constraint violated"
*Cause: an alter table operation tried to validate a check constraint to
populated table that had nocomplying values.
*Action: Obvious
(My territory has nls_numeric_characters='.,', so I've used . rather than ,).
You can either correct or remove the invalid values, or allow the bad values to remain while only validating new data by specifying the novalidate clause:
alter table tableName
add constraint check_height
check (columnName between 1.0 and 2.4)
novalidate;
table TABLENAME altered.
New inserts or updates will still be validated, this only affects existing data.
The decimal place should be separated with a dot . from the fractional part, not a comma, so:
alter table tableName
add constraint check_height
check (columnName between 1.0 and 2.4);
Edit
Regarding comments below - you cannot write a literal number in SQL and PL/SQL using a different decimal point separator than a dot . (documentation). You can alter the session to set the NLS_NUMERIC_CHARACTERS parameter to change the decimal point separator, but it will only be taken into account when converting value of a different type to a number, for example, in the TO_CHAR function.
If you change the NLS_NUMERIC_CHARACTERS, you can use a different separator than a dot . if you enclose the number in quotes (and that will cause an implicit conversion to a number, using the characters set in NLS_NUMERIC_CHARACTERS to determine what is a decimal point, and what is a group separator). So, this will work:
ALTER SESSION SET NLS_NUMERIC_CHARACTERS = ", ";
ALTER TABLE tableName
ADD CONSTRAINT check_height
CHECK (columnName BETWEEN '1,0' AND '2,4');