Converting Int to Real in SQLite

converting int to real in sqlite

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.

Convert integer to text in SQLite's SELECT query?

SQLite supports CAST and:

Casting an INTEGER or REAL value into TEXT renders the value as if via sqlite3_snprintf() except that the resulting TEXT uses the encoding of the database connection.

So you can do things like this:

select cast(some_integer_column as text) from some_table;

Or, depending on what you're trying to do, you could just treat the numbers as strings and let SQLite coerce the types as it sees fit:

select some_int || ' pancakes' from some_table;
select some_int || '' from some_table;

Converting text to int in sqlite when querying

You need to cast in where clause, not where you are selecting it.

string sql4 = "select seq, maxLen from abc where CAST(maxLen as INTEGER) > 30";

Also in your current cast version it will not work since CAST works for a single field.

For your question:

How would I also convert the text to double

cast it to REAL like:

CAST(maxLen as REAL)

How to type cast in sqlite3

The :: syntax is PostgreSQL specific. You could use ANSI standard instead:

SELECT CAST(b AS INT) + 2 AS alias
FROM A

SqlFiddleDemo

.0 appended when converting number to text in SQLITE

You can check whether removing the fractional digits would change the value:

SELECT CAST(CASE WHEN CAST(orderid AS INTEGER) = orderid
THEN CAST(orderid AS INTEGER)
ELSE orderid
END AS TEXT)
FROM orders;


Related Topics



Leave a reply



Submit