Calculate DATEDIFF in POSTGRES using SQLAlchemy

前端 未结 1 1683
野性不改
野性不改 2021-01-12 21:40

I need to calculate DATEDIFF in minutes between 2 columns of timestamp type. There are so many simple examples on the web, but none of them

1条回答
  •  抹茶落季
    2021-01-12 22:03

    PostgreSQL does not have a datediff function. To get the number of minutes, use the SQL expression:

    trunc((extract(epoch FROM newer_date) - extract(epoch FROM older_date)) / 60)
    
    • extract(epoch FROM …) converts the timestamp to number of seconds.
    • / 60 converts to seconds to minutes
    • trunc(…) removes the fractional part.

    So probably try

    sa.func.trunc((
        sa.extract('epoch', datetime.utcnow()) -
        sa.extract('epoch', datetime.utcnow())
    ) / 60)
    

    0 讨论(0)
提交回复
热议问题