How to tweak the SET intervalstyle (change the Interval Output) in PostgreSQL?

白昼怎懂夜的黑 提交于 2020-01-05 19:29:27

问题


I have read in this online PostgreSQL documentation... http://www.postgresql.org/docs/9.4/static/datatype-datetime.html#INTERVAL-STYLE-OUTPUT-TABLE in the point 8.5.5 something about how to tweak the default Interval Output. . I mean the default interval is shown like this...

00:00:00.000 (if the timedifference is lower than a day or month or year)

1 day 00:00:00.000 (if the timedifference reaches days, but is lower than a month or a year)

1 month 1 day 00:00:00.000 (if the timediffence reaches months, but is lower than a year)

1 year 1 month 1 day 00:00:00.000 (if it reaches years, months, days)

it evens uses plurarl cases (years, mons, days) when their values are greater than one.

All these variations make difficult to any other app when SELECTing (query) this interval values (as text) to convert it to a proper time. So I would like postgresql to always show year, month n days, even if their value are 0 (it could be even better if it could show the date part of the interval like this... 01-11-30, adding zeros to the left side when values are less than ten)

I know I can change the interval to text, using to_char() but I really would like to avoid that, I would like some good fellow postgresql programmer to tell me if it is true that there is a way to tweak the Interval Output as is said in the postgresql documentation.

Thanks Advanced.

PD: two more links about the subject https://my.vertica.com/docs/7.1.x/HTML/Content/Authoring/SQLReferenceManual/Statements/SET/SETDATESTYLE.htm http://my.vertica.com/docs/6.1.x/HTML/index.htm#13874.htm


回答1:


You can set the interval output style, but only to one of a few pre-defined formats that are unambigious on input, and that PostgreSQL knows how to parse back into intervals. Per the documentation these are the SQL standard interval format, two variants of PostgreSQL specific syntax, and iso_8601 intervals.

If you want something familiar and easy to parse, consider using:

SET intervalstyle = 'iso_8601'

and using an off-the-shelf parser for ISO1601 intervals.



来源:https://stackoverflow.com/questions/31150577/how-to-tweak-the-set-intervalstyle-change-the-interval-output-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!