varchar column returning exponential representation of number - sql

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)

Related

Convert column type from FLOAT to MONEY in PostgreSQL

I need to change a column type from FLOAT to MONEY, but I get the error:
Link to the example:
ERROR: operator does not exist: money >= double precision
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
Current table:
CREATE TABLE user_settings
(
...
price FLOAT DEFAULT 0 CHECK (price >= 0)
);
Here is the migration part:
ALTER TABLE user_settings
ALTER COLUMN price TYPE money USING price::text::money,
ALTER COLUMN price SET DEFAULT 0.0::money;
ALTER TABLE user_settings ADD CHECK (price >= 0.0::money);
First to numeric then to money:
alter table user_settings
alter column price type numeric using price::numeric::money;
DEMO
Values of the numeric, int, and bigint data types can be cast to
money. Conversion from the real and double precision data types can be
done by casting to numeric first, for example:
SELECT '12.34'::float8::numeric::money;

Redshift won't change column type

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

how to set maximum length to int data type in sql

ALTER TABLE table_name ALTER COLUMN column_name [int] (4) NULL;
unable to execute the script, please assist how can I add maximum length to already existed column for an int data type.
If I understand correctly, you want to add a constraint to the column so that it cannot contain a value larger than 9999:
ALTER TABLE table_name
ADD CONSTRAINT CK_column_name_RANGE CHECK (column_name >= 0 AND column_name <= 9999)
SQL Server's integer data types use binary integers. The INT data type is a 32-bit signed number, which allows values from -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647). You cannot specify the width of such integers, except by choosing TINYINT or SMALLINT.
If you must somehow prevent numbers outside the range that fits in four digits from getting into your table, you can use the DECIMAL data type; it allows you to specify the digit count.
column_name DECIMAL(4,0)

How to set datatype of SQL column in Alias?

If I have a query such as:
select name as first_name
from my_table
How can I set the datatype of that column in Postgresql to be character varying or anything in future - is there a way such as:
select name as first_name character varying
from my_table
I assume there is a way to set a datatype of a column after the aliasing - unsure how in postgresql
I am using postgresql and pgadmin4
Use cast to change the data type:
select cast(name as character varying(100)) as first_name
from my_table
But I'd consider altering the table column instead, to be varchar(100):
alter table my_table alter column name type character varying(100)
Use new datatype for column after TYPE keyword in PostgreSQL
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_datatype;
Let's take an example -
CREATE TABLE Test (
emp_id serial PRIMARY KEY,
emp_name TEXT NOT NULL,
);
INSERT INTO Test (emp_id, emp_name) VALUES ('abc00012','Shubham');
ALTER TABLE Test ALTER COLUMN emp_name TYPE VARCHAR;
In Postgres, I would use the short-hand syntax for type conversion :::
select name::varchar as first_name
Of course, the right thing to do is to fix the type in the table, but that is already covered by other answers.
To easily cast your result to any datatype use :: datatype as in below:
select name as first_name ::varchar from my_table

How to add a column in MS Access using SQL?

Everytime I try to add a column using ALTER and ADD COLUMN I am getting a syntax error.
ALTER TABLE EMP_1
ADD COLUMN EMP_PCT NUMBER(4,2),
ADD COLUMN PROJ_NUM CHAR(3);
I am not familiar with a number data type. Perhaps you mean decimal:
ALTER TABLE EMP_1 ADD COLUMN EMP_PCT DECIMAL(5, 2);
You may also need two ALTER TABLE statements. I am just not sure if MS Access allows two changes in one statement.
For a numeric type you should use NUMERIC, not NUMBER, and for a text type you should use VARCHAR.