Skip to main content

extract

Function extract 

Source
pub fn extract<'a, 'f, V, E>(
    field: &'f str,
    expr: E,
) -> SQLExpr<'a, V, <V::DialectMarker as DialectTypes>::Double, E::Nullable, E::Aggregate, E::Sources>
where V: SQLParam + 'a, V::DialectMarker: DialectSupports<PostgresDateTime>, E: Expr<'a, V>, E::SQLType: Temporal, 'f: 'a,
Expand description

One field of a time value (EXTRACT(field FROM expr)), on PostgreSQL.

field is written into the SQL as is, so pass a fixed field name such as "YEAR", "MONTH", "DAY", "HOUR", "DOW" or "EPOCH", never user input. expr must be temporal. PostgreSQL 14+ returns numeric, so the call is cast to DOUBLE PRECISION; the result is float8 and keeps expr’s nullability.

§Examples

let year = extract("YEAR", users.created_at);
assert_eq!(
    year.sql(),
    r#"CAST (EXTRACT( YEAR FROM "users"."created_at") AS DOUBLE PRECISION)"#
);