Type Casting with CAST
Sometimes data isn't in the type you need. CAST converts values between types.
Basic syntax: CAST(value AS type)
Common conversions: CAST(salary AS TEXT) -- number to text CAST('42' AS INTEGER) -- text to number CAST(price AS REAL) -- integer to decimal
Useful when dividing integers (avoids integer division):
SELECT CAST(total AS REAL) / count AS average
FROM stats;Without CAST, 7 / 2 = 3 (integer division). With CAST, 7.0 / 2 = 3.5 (decimal division).
Check your understanding
How do you convert a value to a decimal type in SQLite?