Type conversion functions

Cast

class piccolo.query.functions.type_conversion.Cast(identifier: Column | QueryString, as_type: Column, alias: str | None = None)

Cast a value to a different type. For example:

>>> from piccolo.query.functions import Cast

>>> await Concert.select(
...     Cast(Concert.starts, Time(), "start_time")
... )
[{"start_time": datetime.time(19, 0)}]
Parameters:
  • identifier – Identifies what is being converted (e.g. a column).

  • as_type – The type to be converted to.

Notes on databases

Postgres and CockroachDB have very rich type systems, and you can convert between most types. SQLite is more limited.

The following query will work in Postgres / Cockroach, but you might get unexpected results in SQLite, because it doesn’t have a native TIME column type:

>>> from piccolo.columns import Time
>>> from piccolo.query.functions import Cast
>>> await Concert.select(Cast(Concert.starts, Time()))