Double rather than Int
(1) By alfansome (terrywarren) on 2022-11-12 18:06:28 [link] [source]
I have a database table in which there is a column defined as Real(4,2); When I fetch values for this column and look at the column type value it is sometimes Double and sometimes Int, I guess depending on whether or not the stored value can be considered an Int or not. (EG if I store the value 3.5 it is typed as Double, but if I store the value 3.0 it is typed is Int).
My question is: is there any way to "force" sqlite3 to always return the value as Double? As a workaround, I created a VIEW on the table in which I select all the row column values but if I want the REAL value to always be Double I select it as 1.0*Column AS RColumn instead of just Column; then RColumn will always be Double.
(2) By SeverKetor on 2022-11-12 18:24:49 in reply to 1 [link] [source]
Define the column as a type that actually exists: Real.
The extra (4,2) does nothing for SQLite.
(3) By Keith Medcalf (kmedcalf) on 2022-11-12 18:32:25 in reply to 1 [link] [source]
You are probably using an ancient version of SQLite from before this was fixed several years ago -- and you failed to state the version in which you see this occur.
If you use the "current versions" do you see the same behaviour?
Note that the response that the typename is incorrect is incorrect. It is true that the blathering "(4,2)" is ignored, but so would be other blatherings like having the column declared as "quacking real duck" (aka col quacking real duck
).
(4) By alfansome (terrywarren) on 2022-11-12 23:42:37 in reply to 3 [link] [source]
Thanks for the replies!
I am using a current version: macOS 3.39.4 2022-09-07; however I had been experimenting with different affinities and the results I posted were actually using "decimal(4,2)" rather than "real". When I switch to "real" then the values are retrieved as I expected them to be (IE 4.5 is displayed as 4.5 and 3 is displayed as 3.0). Sorry for any confusion.
(5) By anonymous on 2022-11-14 01:35:47 in reply to 4 [source]
Given your OP requirement of forcing the value to be a floating point (real) you may wish to look at using STRICT tables (in addition to paying close attention to the column type as per the other replies).
https://www.sqlite.org/stricttables.html
(6) By Gunter Hick (gunter_hick) on 2022-11-14 07:01:56 in reply to 4 [link] [source]
As per https://sqlite.org/datatype3.html section 3.1 the affinity of the given type decimal(4,2) is NUMERIC. If it looks like an integer, SQLite will store an INTEGER; if it looks like a floating point value, SQLite will store a FLOAT; anything else will be stored as TEXT.
(7.3) By Keith Medcalf (kmedcalf) on 2022-11-14 15:51:50 edited from 7.2 in reply to 6 [link] [source]
I think what you mean to say is that if the affinity is NUMERIC, then an integer will be stored as an integer, a real (floating point) will be stored as an integer if the fractional part is zero or as a real if the fractional part is not zero;
text -- if it looks like an integer (the fractional part is 0) will be stored as an integer, if it looks like floating point (has a non-zero fractional part) will be stored as real, otherwise it will be stored as text. A blob will be stored as a blob. NULL will be stored as a NULL.
"Anything else will be stored as text" is incorrect.
sqlite> create table x(x numeric);
sqlite> insert into x values (1), (2.0), (3.1), ('text'), ('12'), ('14.1'), ('15.0'), (NULL), (x'31');
sqlite> select * from x;
┌────────┐
│ x │
├────────┤
│ 1 │
│ 2 │
│ 3.1 │
│ 'text' │
│ 12 │
│ 14.1 │
│ 15 │
│ NULL │
│ x'31' │
└────────┘
That's .mode qbox
by the way, which makes it visually apparent that you are seeing what you get ...
This makes it even more apparent:
sqlite> select x, typeof(x) from x;
┌────────┬───────────┐
│ x │ typeof(x) │
├────────┼───────────┤
│ 1 │ 'integer' │
│ 2 │ 'integer' │
│ 3.1 │ 'real' │
│ 'text' │ 'text' │
│ 12 │ 'integer' │
│ 14.1 │ 'real' │
│ 15 │ 'integer' │
│ NULL │ 'null' │
│ x'31' │ 'blob' │
└────────┴───────────┘
(8) By Gunter Hick (gunter_hick) on 2022-11-14 15:50:55 in reply to 7.1 [link] [source]
Sorry I was not clear enough and left out BLOB and NULL values. The preferences still hold regarding INT, REAL and TEXT types; obviously, an INT always "looks like an integer", and gets stored as such; if a REAL "looks like" an integer (no fractional part and in range) it is stored as an INT; only a character can fail to "look numeric" and get stored as TEXT.