Skip to contents

Provides the sql code to compute the age of a person on a given date.

Usage

age_on_date(birth, reference_date, conn)

Arguments

birth

(character(1))
Name of the birth date column.

reference_date

(Date(1) or character(1))
The date to compute the age for (or name of column containing the reference date).

conn

(DBIConnection)
A database connection.

Value

SQL query that computes the age on the given date.

Examples

  conn <- SCDB::get_connection(drv = RSQLite::SQLite())

  dplyr::copy_to(conn, data.frame(birth = as.Date("2001-04-03"), "test_age")) |>
    dplyr::mutate(age = !!age_on_date("birth", as.Date("2024-02-28"), conn))
#> Warning: Age computation on SQLite is not precise. For some edge-cases, age will be off by 1 year! Consider using DuckDB as a local database with precise age computation.
#> Warning: Age computation on SQLite is not precise. For some edge-cases, age will be off by 1 year! Consider using DuckDB as a local database with precise age computation.
#> Warning: Age computation on SQLite is not precise. For some edge-cases, age will be off by 1 year! Consider using DuckDB as a local database with precise age computation.
#> # Source:   SQL [1 x 3]
#> # Database: sqlite 3.47.1 [:memory:]
#>   birth X.test_age.   age
#>   <dbl> <chr>       <int>
#> 1 11415 test_age       22

  DBI::dbDisconnect(conn)