Division in sqlite return integer value
sqlite> select totalUsers/totalBids from (select (select count(*) from Bids) as totalBids , (select count(*) from Users) as totalUsers) A; 1
Can we typecast the result to get the real value of division result?
SQLite CAST operator: The CAST operator is used to convert a value from a data type to another data type. For example, if you have a numeric value stored as a string value like this ” '12.5' ” and you want to convert it to be a numeric value you can use the CAST operator to do this like this “CAST( '12.5' AS REAL)“.
The SQLite multiply ( * ) operator is used to multiplying two or more expressions or numbers. Here is the result. The SQLite divide ( / ) operator is used to divide one expression or numbers by another. 1.
SQLite Storage Classes INTEGER – any numeric value is stored as a signed integer value (It can hold both positive and negative integer values). The INTEGER values in SQLite are stored in either 1, 2, 3, 4, 6, or 8 bytes of storage depending on the value of the number.
You can declare a VARCHAR(10) and SQLite will be happy to store a 500-million character string there. And it will keep all 500-million characters intact. Your content is never truncated. SQLite understands the column type of "VARCHAR(N)" to be the same as "TEXT", regardless of the value of N.
Just multiply one of the numbers by 1.0
:
SELECT something*1.0/total FROM somewhere
That will give you floating point division instead of integer division.
In Sqlite the division of an integer by another integer will always round down to the closest integer.
Therefore if you cast your enumerator to a float:
SELECT CAST(field1 AS FLOAT) / field2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With