Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting int to real in sqlite

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?

like image 853
vaichidrewar Avatar asked Nov 29 '11 03:11

vaichidrewar


People also ask

What is cast in SQLite?

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

How do you divide in SQLite?

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.

What is numeric SQLite?

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.

Does SQLite support varchar?

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.


2 Answers

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.

like image 82
NullUserException Avatar answered Oct 17 '22 16:10

NullUserException


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 
like image 25
Adam Garner Avatar answered Oct 17 '22 15:10

Adam Garner