0

Is there any aggregate function to format date & time (however I am interested in interval more) in a custom way like 11:10:45 to 11h 10m 45s or something like that in PostgreSQL ? or I need to extract the parts or concat them by myself ?

Problem occurs specially with number of days in interval

4

3 回答 3

1

你的问题不是很清楚。如果你想找回

间隔天数

您可以使用EXTRACT

SELECT EXTRACT(day from '7 week'::interval) AS d

 d
----
 49

请注意,这会提取给定区间的子字段。间隔未标准化或合理化。为了证明间隔首先使用justify_interval()

SELECT EXTRACT(day from justify_interval('7 week'::interval)) AS d;

 d
----
 19

从 49 天中扣除了一个月的 30 天。

因此,这些表达式返回 0 天:

SELECT EXTRACT(day from '24h'::interval);
SELECT EXTRACT(day from '1 months'::interval);

如果您只想要日期之间的天数

SELECT '2012-3-23'::date - '2012-3-1'::date AS d;

 d
----
 22

您只需减去日期即可得到一个整数,表示两者之间的天数。

或在时间戳之间:

SELECT EXTRACT (day from '2012-03-25 23:12'::timestamp
                       - '2010-03-01 03:34'::timestamp) AS d;

  d
-----
 755
于 2012-04-30T23:32:23.087 回答
1

查看 to_char 函数的完整文档,它可以做你想做的事

http://www.postgresql.org/docs/9.1/interactive/functions-formatting.html

于 2012-04-30T19:46:24.223 回答
0

尝试设置 PostgreSQL 设置intervalstyle的不同值:

db=# show intervalstyle ;
 IntervalStyle 
---------------
 postgres

db=# select interval '3 days 11:10:45';
    interval     
-----------------
 3 days 11:10:45

db=# set intervalstyle = 'postgres_verbose';
SET

db=# select interval '3 days 11:10:45';
             interval              
-----------------------------------
 @ 3 days 11 hours 10 mins 45 secs

db=# set intervalstyle = 'iso_8601';
SET

db=# select interval '3 days 11:10:45';
   interval    
---------------
 P3DT11H10M45S
于 2012-04-30T20:19:04.410 回答