LoginSignup
7
3

More than 3 years have passed since last update.

MySQL BigQuery 誕生日から年齢を取得する関数

Posted at

概要

誕生日から年齢を取得する。誕生日を迎えれば年齢が上がるようにする。

誕生日 年齢
2008-12-16 10
1978-12-30 40
1981-01-22 38

こんな感じに書いていたが、便利な関数があったので紹介!


FLOOR((current_date() - u.birthday) / 10000) age

MySQL


SELECT
  u.birthday
  ,TIMESTAMPDIFF(YEAR, u.birthday, CURDATE()) age
  -- TIMESTAMPDIFF(unit, datetime_expr1, datetime_expr2)
FROM
  users u

BigQuery


SELECT
  u.birthday
  ,DATE_DIFF(current_date(), u.birthday, YEAR) age
  -- DATE_DIFF(date_expression, date_expression, date_part)
FROM
  users u
7
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3