How to save decimal value in TDengine? - tdengine

From the document I didn't see Decimal type support. How can I save decimal data in TDengine? Should I use varchar type (string type in TDengine)?

TDengine definitely supports storing floating numbers as FLOAT/DOUBLE type columns. You can specify the column type when creating table, something like:
create table tb (ts timestamp, col0 float, col1 double)
where col0 and col1 are columns created and can be used to store decimal values.
If you want to preserve decimal digits using string type(binary/nchar) is also a good choice and you can also use sql function cast to convert string values to numerical values.

Related

How to convert decimal values in tofixed format in postgresql

My table column in decimal format like this
My column value is:
4523
1236.23
1240.5
2535.2
I would like to this format:
4523.00
1236.33
1240.50
2535.20
Thanks.
to_char.
SELECT to_char(1231.123, '9999999999.00');
See: data type formatting functions.
The manual is your friend.

SQlite NUMERIC data types size

I've been testing out sqlite3 types for efficiency on a mobile app but I'm getting strange behavior.
I created a table this way:
create table numeric_t(int_4 INTEGER(4), int_8 INTEGER(8), real_4 REAL(4), real_8 REAL(8), new_real DOUBLE, int_1 INTEGER(1));
And am setting values and seing which rows show how many decimals and so on...
Was I wrong to assume that:
- the INTEGER columns should not be able to store numbers with decimals?
- the INTEGER(1), INTEGER(4) and REAL(4) should not be able to store as many decimal places as the others?
Thanks
Check out this snapshot of my results.
In SQLite, column types do not restrict data types:
> CREATE TABLE t(i INTEGER);
> INSERT INTO T VALUES(1);
> INSERT INTO T VALUES(1.23);
> INSERT INTO T VALUES('forty-two');
> SELECT typeof(i) FROM t;
integer
real
text
To quote the documentation:
SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is associated with the value itself, not with its container.
[...]
Any column ... may be used to store a value of any storage class.

Error in storing values in SQL database table

In my table, there is a column called zipcode whose datatype is int. And when I am storing a zipcode which starts with 0 (for eg. 08872), it is getting stored as 8872.
Can anybody explain me why is it happening?
An INT value is numeric - and numerically, 08872 and 8872 are identical - both represent the value 8872.
SQL Server will not store leading zeroes for numerical values. That's just the way it is.
Either store this as CHAR(5) instead, or handle the formatting (adding leading zeroes to your zip codes) on the frontend when you need to display it.

sql server 2000 instance specific setting to cause varchar to int error

I am having an inconsistent issue being produced on one of my servers whereby I have the following
Select *
from SomeVarcharTable v
join SomeIntTable i on i.MyInt=v.MyVarchar
Where v.Id = SomeID
The "MyVarChar" column is surprisingly of type varchar, and "MyInt" is of type int.
Whats curious is that when I run this on my development instance of Sql 2000, I receive no error.
When I run this on my Live instance of Sql 2000 I receive the following error:
Syntax error converting the varchar value '1.4' to a column of data type int.
Does anyone know of any server settings which might cause this. I have gone so far as to do a restore of my production database to my development system and the error doesn't occur.
I wondered whether it was something to do with the sql query engine, but this was working without an issue a few days ago.
Also, I am aware that this is not best practice, and that I could fix the above query by this:
Select *
from SomeVarcharTable v
join SomeIntTable i on cast(i.MyInt as varchar)=v.MyVarchar
Where v.Id = SomeID
I am more interested at this stage in wondering why the issue has been caused, so I can educate others in my company
Thanks in advance
Mark.
This is a data issue. The live environment contains data in the varchar column that is not a valid integer, whereas presumably your dev box doesn't and when the join is being done, it is attempting to convert the VARCHAR to an INTEGER to match on the MyInt column. If MyVarchar contains any value that is not a valid integer, then you will get that error.
The issue is you are storing values in VARCHAR that you are treating in your query as being a valid INTEGER. The fact you are storing them in a VARCHAR instead of an INTEGER column, which of course would be the ideal, indicates in your system that they are not intended to always be valid integers and that's a problem.
So, you either need to:
1) change MyVarchar to be an integer column and only allow valid ints to be stored
2) CAST MyInt to a VARCHAR in the join as you are doing (not great for performance)
3) don't try to join when MyVarchar is not a valid integer (equally not great for performance)
e.g.
Select *
from SomeVarcharTable v
join SomeIntTable i on i.MyInt=v.MyVarchar
Where v.Id = SomeID
AND v.MyVarchar NOT LIKE '%[^0-9]%' -- ignore rows where MyVarchar is not a valid int
4) create a new INTEGER column, alongside MyVarchar, and store the MyVarchar values in there that are valid integers, and use that to join instead. You'd be having some duplicate data, but performance-wise you'd at least be able to index and join on matching types without any workarounds.
Update:
Check out this reference on data type precedence.
Note INTEGER has a higher precedence than VARCHAR, hence is why VARCHARs are converted to INTEGERs.
I believe, when you are trying to compare a left hand INT value with the right side VARCHAR, the sql tries to implicitly convert the VARCHAR to an INT value and then compare. If it cannot implicitly convert varchar to INT, then it throws the above error which you are encountering.
integer data type has a higher precedence over a varchar data type. Since the integer data type has a higher precedence, the varchar data type is implicitly converted by SQL Server to an integer data type, and not the other way around.

How do you use a binarywriter to write the correct MS SQL native format for the Money data type?

How do you use a binarywriter to write the correct MS SQL native format for the Money data type?
I'd like to take a value in .net, read from a file as string representation of a decimal amount (actually an exported "Money" data type from SQL, but that is unimportant).
How can I use a binary writer to write the value so that you can use BCP or BULK INSERT in native format mode to read the value in successfully?
(where w is a binary writer previously instantiated)
Dim dec = CDec(aString)
Dim lng = CLng(dec * 10000)
Dim bytes = BitConverter.GetBytes(lng)
w.Write(bytes(4))
w.Write(bytes(5))
w.Write(bytes(6))
w.Write(bytes(7))
w.Write(bytes(0))
w.Write(bytes(1))
w.Write(bytes(2))
w.Write(bytes(3))
There may be cleaner or better ways, but this seems to be ok
as a heads up, this format is only for NON NULL money columns, I think you have to write a length byte first or something to that effect for nullable money columns
I don't recall how to do null values on numeric types in delimited files with BCP but I believe if you use a length-prefixed file, a length value of -1 denotes a null value.

Resources