Last Updated: February 25, 2016
·
651
· jraines

Getting a "Days Since" computed column in Postgres

When writing a query, especially for a dashboard, you might have a date column but want to show the distance between that date and now, instead of just the date. For example: Days Since Last Signin is more immediately useful than Last Signin Date.

To get that in Posgres, you can use the EXTRACT and AGE functions like so:

extract(days from age(current_timestamp, users.current_sign_in_at)) as days_since_signin,