Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer to text in SQLite's SELECT query?

I would like to create a SELECT query that would return numbers from column in integer format as a text format - can I do it in SQLite?

like image 218
syntagma Avatar asked Sep 14 '12 07:09

syntagma


People also ask

Can you convert data types in SQL?

Data types can be converted either implicitly or explicitly. Implicit conversions are not visible to the user. SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds.


1 Answers

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; 
like image 142
mu is too short Avatar answered Sep 23 '22 03:09

mu is too short