Thanks to a last minute client request a integer field in our database now needs to be a decimal, to two points.A value of 23 should become 23.00.
Is there a nice way I can convert the table and cast the data across?
I'll freely admit, I haven't done anything like this with PostgreSQL before so please be gentle with me.
Something like this should work:
alter table t alter column c type decimal(10,2);
Edit:
As #Oli stated in the comments; the first number is the entire length of the number (excluding the point) so the maxval for (10,2) would be 99999999.99
alter table table_name alter column columname type decimal(16,2);
for converting data type from int to decimal. with precession after decimal point 2 values it will come for example 10.285 then it will be 10.23.or in decimal place we can use numeric also it will work.
Related
I have a table with a column defined as follows:
PrincipalBalance DECIMAL(13,2) NOT NULL
I have a stored procedure that returns that value as follows:
SELECT D.PrincipalBalance
FROM MyTable AS D
WHERE ID = #myKey;
If I inspect the metadata for the table, the column definition is correct (it shows as DECIMAL(13,2).) It shows with the correct type in Object Explorer as well. However, IntelliSense for the column in the stored procedure shows that it is an INT, which is thoroughly puzzling.
The only workaround I have found for this is to use a CAST/CONVERT in the stored procedure, which seems like it should be unnecessary.
Note Earlier today I experienced the same problem with a decimal column that the was being returned as a VARCHAR(50) (or some other relevant length). It's interesting that in both cases, the value of the decimal column was zero.
What is going on here? Why is SQL Server selecting the wrong type?
I have a certain file type that contains a column with floats which I read in using insert
`table insert ("TISISIFIIIFFIbIFFFFFFIIIFIIFFFFIIIIIIIIIIIIFFFFFFIIFFIIIIFFIIIIIIIIIIIIIIIII"; enlist "\t" ) 0:`:my_file.txt
unfortunately, sometimes the values in column happen to be all integers and in the txt file the are saved as ints, not floats, so as 1 instead of 1.0 and it seems kdb is throwing a type error. Is there a way make kdb accept ints saved in that format as floats?
I do have a lot of columns with floats and theoretically, the problem can appear in any of them. Is there some way to tell kdb on insert to treat any int as float if the column type is float?
The 'type error is actually happening from your insert. You are trying to insert some parsed data, but the types of each column are not conforming to the types of each column in 'table'.
You are basically saying that your raw data can contain floats, therefore you are going to have to read them in as floats.
What you do with that column after the parse is up to you though.
1) keep as floats, read in as floats, insert as floats, column should be a float in 'table' pre-read (I presume this is what you want going by your question):
update "f"$COLUMN from `table
`table insert (1#"F";1#"\t") 0:`myfile.txt
2) update to an integer and then insert into 'table' - you are going to have update the schema of table first, read in as floats, and then run an update after every read:
update "i"$COLUMN from `table
`table insert update "i"COLUMN from (1#"F";1#"\t") 0:`myfile.txt
Another option you may want to consider, but please test first as it may replace too much, is to replace the trailing ".0" from your floats, and then just read in as integers:
q)\cd /var/tmp
q)`:myfile.txt 0:("x\tx1";"1.0\t2.0";"3.0\t1.0")
q)\sed -i -e 's/.0//g' myfile.txt
q)("II";1#"\t")0:`myfile.txt
So i learned in my Sql course last week how to turn a string into an integer. the table we used for this was timezone based. so it was '-5' hours offset.
in order to do this we had to cast the string to a DECIMAL and then to an SMALLINT. It was pretty simple once I knew that , thats not where my question lies.
what im curious about is why a SMALLlNT wouldnt take a negative sign but A Decimal could do it. according to the specs a SMALLINT still can go to -32768. so does anyone know if this persists in all coding languages or is it just SQL specific? As well as what wont allow it to cast
I don't see why you would bother doing any casting to begin with? According to the documentation (see table 1/3 down the page), T-SQL supports implicit conversion between varchar and smallint.
DECLARE #negative_varchar VARCHAR(10) = '-5'
DECLARE #negative_smallint SMALLINT = CONVERT(SMALLINT, #negative_varchar)
DECLARE #negative_smallint_implicit SMALLINT = #negative_varchar
SELECT #negative_varchar, #negative_smallint, #negative_smallint_implicit
Produces
---------- ------ ------
-5 -5 -5
no this is what im saying (declare #s as nvarchar
#s=-5
(50) = CAST (#s AS SMALL INT)
again you have to cast from decimal then small int. im asking about the underlying code behind the process.
such as does one of the bytes hold whether a number is positive or negative etc...
I have a table named "temp_table" and a column named "temp_column" of type varchar. The problem is "temp_column" must be of type integer. If I will just automatically update the table into type integer, it will generate an error since some data has non-numeric data in it.
I want a query that will show all rows if "temp_column" has non-numeric values in it (or the other way around) and update or SET the value accordingly. I'm having a hard time since ISNUMERIC is not available in postgresql.
how to do this?
This will show all rows where you have non-integer values in that column. It uses a regular expression to find all values that have anything else than just numbers in it:
select *
from temp_table
where temp_column ~ '[^0-9]';
this can also be used in an update statement:
update temp_table
set temp_column = null
where temp_column ~ '[^0-9]';
This will also filter out "numeric" values like 3.14 as those aren't integers.
I'm working with postgresql-9.1 recently.
For some reason I have to use a tech which does not support data type numeric but decimal. Unfortunately, the data type of columns which I've assigned decimal to them in my Postgresql are always numeric. I tried to alter the type, but it did not work though I've got the messages just like "Query returned successfully with no result in 12 ms".
SO, I want to know how can I get the columns to be decimal.
Any help will be highly appreciate.
e.g.
My creating clauses:
CREATE TABLE IF NOT EXISTS htest
(
dsizemin decimal(8,3) NOT NULL,
dsizemax decimal(8,3) NOT NULL,
hidentifier character varying(10) NOT NULL,
tgrade character varying(10) NOT NULL,
fdvalue decimal(8,3),
CONSTRAINT htest_pkey PRIMARY KEY (dsizemin , dsizemax , hidentifier , tgrade )
);
My altering clauses:
ALTER TABLE htest
ALTER COLUMN dsizemin TYPE decimal(8,3);
But it does not work.
In PostgreSQL, "decimal" is an alias for "numeric" which poses some problems when your app thinks it expects a type called "decimal" from the database. As Craig noted above, you can't even create a domain called "decimal"
There is no good workaround in the database side. The only thing you can do is change the application to expect a numeric data type back.
Use Numeric (precision, scale) to store decimals
precision represents the total number of expected digits on either side of the decimal point. scale is the number decimals you wish to store.
This Numeric (5,5) would imply you only want numbers less than 1 (negative or positive) with 5 decimal points. Debug, it may be Numeric (6,5) if the postgre sql errors out because it things the leading 0 is a decimal.
0.12345 would be an example of the above.
1.12345 would need a field Numeric (6,5)
100.12345 would need a field Numeric (8,5)
-100.12345 would need a field Numeric (8,5)
When you write a select statement to see the decimals, it rounds to 2; but if you do something like Select 100 * [field] from [table], then extra decimals should start appearing....