How to add number of days in postgresql datetime

前端 未结 2 1669
无人及你
无人及你 2020-12-23 18:28

I have a following table projects.

id title        created_at                     claim_window
1  Project One  2012-05-08 13:50:09.924437     5
         


        
相关标签:
2条回答
  • 2020-12-23 19:07

    For me I had to put the whole interval in single quotes not just the value of the interval.

    select id,  
       title,
       created_at + interval '1 day' * claim_window as deadline from projects   
    

    Instead of

    select id,  
       title,
       created_at + interval '1' day * claim_window as deadline from projects   
    

    Postgres Date/Time Functions

    0 讨论(0)
  • 2020-12-23 19:22

    This will give you the deadline :

    select id,  
           title,
           created_at + interval '1' day * claim_window as deadline
    from projects
    

    Alternatively the function make_interval can be used:

    select id,  
           title,
           created_at + make_interval(days => claim_window) as deadline
    from projects
    

    To get all projects where the deadline is over, use:

    select *
    from (
      select id, 
             created_at + interval '1' day * claim_window as deadline
      from projects
    ) t
    where localtimestamp at time zone 'UTC' > deadline
    
    0 讨论(0)
提交回复
热议问题