Astyanax Cassandra Double type precision - cql3

I'm trying to get a Double value from a Cassandra table with a double type column. I've created the table in CQL3 syntax:
CREATE TABLE data_double (
datetime timestamp,
value double,
primary key (datetime)
);
I've inserted a row:
INSERT INTO data_double (datetime, value) VALUES ('111111111', 123.456);
When I do:
SELECT * from data_double;
I get that the value is 123.46
Why is the value rounded?
Thanks

The cqlsh utility by default will only display 5 digits of precision for floating point numbers.
You can increase this by creating a cqlshrc file with contents like the following:
[ui]
float_precision = 10
The cqlsh docs page provides more details on the available options.
Update: The location of the cqlshrc file changed at some point in Cassandra. The new default location is ~/.cassandra/cqlshrc. You can also use a file in a different location with the --cqlshrc command-line option.

Related

Postgresql drops trailing zeroes when loading time with milliseconds from csv

I am importing a csv file into a Postgres Table. The file has the following format:
2019/12/13, 14:56:02, 3172.50, 3174.25, 3172.50, 3172.50, 1, 1, 1, 0
The table is defined as:
CREATE TABLE tablename (
date date,
time time,
v1 numeric,
v2 numeric,
v3 numeric,
v4 numeric,
v5 integer,
v6 integer,
v6 integer,
v7 integer,
PRIMARY KEY(date, time)
);
There is an issue with the time field. In some cases, milliseconds are added for precision:
14:56:02.1
14:56:02.9
14:56:02.10
Unfortunately, Postgres seems to drop the trailing zero, which causes it to mark below two values as duplicates:
14:56:02.1
14:56:02.10
ERROR: duplicate key value violates unique constraint "tablename_pkey"
DETAIL: Key (date, "time")=(2019-12-13, 14:56:02.1) already exists.
CONTEXT: COPY input_file, line 1584
Is there a way to instruct psql not to drop trailing zeroes? I tried time(4) to enforce 4 digit precision, with no difference.
Thanks!
Postgres is not doing anything wrong here. It took me a moment to realize that the issue is with the data.
.1 and .10 are equal. In the data, the timestamp was used creatively, i.e. in this case .1 means "1st record within this second" and .10 means "10th record within this second", so the millisecond component didn't make sense from timestamp's point of view.

Why is T-SQL converting my DECIMAL data to INT?

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?

How can I assign a data type decimal to a column in Postgresql?

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....

T-SQL: Disable default FLOAT rounding when using Table with its View

Good day!
Here is the problem of float rounding when I want insert data into Table using its View.
Data NEED to store as varchar in Table.
Table:
CREATE TABLE [dbo].[TestTableFloat_E]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[FloatField] [varchar](100) NOT NULL
)
View:
ALTER VIEW [dbo].[TestTableFloat]
AS
SELECT id
,Convert(float, FloatField) as FloatField
FROM dbo.TestTableFloat_E
The data, what I select from Table using its View - have to have float type (field FloatField).
I can't insert data into Table, if i need, I need do it with its View. So, I can't (this is task rule) insert data directly into Table, just with View. I create trigger to insert data:
CREATE TRIGGER [dbo].[tr_TestTableFloatInsert]
ON [dbo].[TestTableFloat]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
insert into TestTableFloat_E (FloatField)
select FloatField
FROM INSERTED
Actions: I try insert data into TestTableFloat_E (table) with help of its View (TestTableFloat), rises trigger and insert data into table.
Problem: When I insert float number, I have rounding, that I don't need:
insert TestTableFloat (FloatField)
select 123.123456
I have 123,123 in the Table TesttableFloat. I need it doesn't round, I have to have 123.123456
What can it be?
You can have it store more digits, but it's going to switch to scientific notation:
CREATE TRIGGER [dbo].[tr_TestTableFloatInsert]
ON [dbo].[TestTableFloat]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
insert into TestTableFloat_E (FloatField)
select CONVERT(varchar(100),FloatField,2)
FROM INSERTED
END
Which ends up storing 1.231234560000000e+002 into the table.
From CAST and CONVERT:
When expression is float or real, style can be one of the values shown in the following table. Other values are processed as 0.
0 (default)
A maximum of 6 digits. Use in scientific notation, when appropriate.
1
Always 8 digits. Always use in scientific notation.
2
Always 16 digits. Always use in scientific notation.
Insert usual caveats about futility of expecting float and a decimal representation of the same to always be exactly convertible, and of using inappropriate data types to store particular types of data
It would appear the best data type to use for this data would be decimal with appropriate scale and precision, which is neither of the types you're working with. But you claim that those are the "required" types. E.g. if the view uses a decimal instead of a float, the stored varchar(100) value is exactly as expected.

Alter PostgreSQL column from integer to decimal

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.