I want to convert both of the following columns to integer (they were placed as text in the SQlite db) as soon as I select them.
string sql4 = "select seq, maxLen from abc where maxLen > 30";
I think it might be done like this..(using cast)
string sql4 = "select cast( seq as int, maxLen as int) from abc where maxLen > 30";
Not sure if it's right as I seem to be getting a syntax error.
How would I also convert the text to double
TO_NUMBER converts a string to a number of data type NUMERIC. TO_CHAR performs the reverse operation; it converts a number to a string. CAST and CONVERT can be used to convert a string to a number of any data type. For example, you can convert a string to a number of data type INTEGER.
There is no difference between a NUMERIC value which is an integer and an INTEGER value which is an INTEGER -- they are both INTEGER and behave exactly the same.
Some Differences Between VARCHAR and TEXT The VAR in VARCHAR means that you can set the max size to anything between 1 and 65,535. TEXT fields have a fixed max size of 65,535 characters. A VARCHAR can be part of an index whereas a TEXT field requires you to specify a prefix length, which can be part of an index.
For one thing, databases vary considerably in how they handle text; for example, while some databases are case-sensitive by default (e.g. Sqlite, PostgreSQL), others are case-insensitive (SQL Server, MySQL).
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)
Syntax issue is you're putting two casts in one cast clause. Try this:
string sql4 = "select cast(seq as int), cast(maxLen as int)
from abc where maxLen > 30"
Like Habib pointed out, you should typecast the where clause, otherwise the comparison isn't numeric but textual.
string sql4 = "select cast(seq as int), cast(maxLen as int)
from abc where cast(maxLen as int) > 30"
And also, casting to float is simple use float instead of int (or you can use REAL which is same datatype in SQLite)
cast(maxLen as float)
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