converting int to real in sqlite

SqlSqliteCastingDivision

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

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.

Solution 2 - Sql

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

Solution 3 - Sql

select cast ( ( select 1 ) as real );

https://www.sqlite.org/lang_expr.html#castexpr

Solution 4 - Sql

or if you want to update column based on text column:

UPDATE table_with_fields SET real_field=cast(field_with_txt AS real)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvaichidrewarView Question on Stackoverflow
Solution 1 - SqlNullUserExceptionView Answer on Stackoverflow
Solution 2 - SqlAdam GarnerView Answer on Stackoverflow
Solution 3 - SqlmpbView Answer on Stackoverflow
Solution 4 - SqlGregView Answer on Stackoverflow