Help Center/ GaussDB/ Developer Guide(Distributed_8.x)/ FAQs/ How do I query the time difference between two time points?
Updated on 2024-06-03 GMT+08:00

How do I query the time difference between two time points?

Answer: You can use the following method to calculate the time difference:
  • Use the age() function to calculate the time difference between two time points.
    gaussdb=# SELECT age(timestamp '2001-04-10 14:00:00', timestamp '2001-04-06 13:00:00');
           age       
    -----------------
     4 days 01:00:00
    (1 row)
  • Convert strings to the date type and then subtract them.
    gaussdb=# SELECT ('2001-04-10 14:00:00'::date - '2001-04-06 13:00:00'::date);
     ?column? 
    ----------
            4
    (1 row)
  • Use the date_part() function to obtain the value of the subdomain in a date or time value.
    gaussdb=# SELECT date_part('day', '2001-04-10 14:00:00'::timestamp - '2001-04-06 13:00:00'::timestamp);
     date_part 
    -----------
             4
    (1 row)