PostgreSQL: exists robust third party date-math functions to augment the built-in date operators?

StackOverflow https://stackoverflow.com/questions/14604953

  •  06-03-2022
  •  | 
  •  

質問

I'm porting some T-SQL stored procs to PL/pgSql and, being very new to PostgreSQL, don't know what helpful utility functions might be available in the pg community. Is there a set of robust date-math functions that "nearly everybody uses" out there somewhere? I don't want to quickly cobble together some date-math functions if there's already a great package out there.

The PostgreSQL date math operators with "natural language" string literal arguments are user-friendly if you're typing a query and you happen to know the interval:

      select now() - interval '1 day'

but if the interval 1 is the result of a calculation involving nested date-math function calls, these string literals are actually not very user-friendly at all, and it would easier to work with a date_add function:

      select dateadd(d, {calculation that returns the interval}, now() )

Thanks

役に立ちましたか?

解決

Let me give you an example. I want to subtract from an arbitrary date the number of months that have elapsed since 1/1/1970, and then add that number of months to 1/1/1970 to return the first day of the month in which the arbitrary date falls

select (date_trunc('month', '2013-01-30'::date))::date

Or add a month to the first day of this month to get the first day of the next month, then subtract one day to get the last day of this month

select date_trunc('month', '2013-01-30'::date + 1 * interval '1 month')::date - 1

Notice in the above example you can add any number of months by multiplying the interval '1 month' by an integer. You can do that with any interval without manipulating the string '1 month'. So to add or subtract any interval you just:

select current_date + 5 * interval '1 month'

No need for messy string manipulations. You can multiply by fractions also:

select current_timestamp + 3.5 * interval '1 minute'

To add or subtract days to a date type you use an integer:

select current_date + 10

他のヒント

The "natural language" strings you're talking about are interval literals. Intervals can also be obtained by using date arithmetic.

Surely dateadd can be quite simply emulated in Postgresql as follows:

select d + ({calculation the returns the interval}::text || ' day')::interval

Substitute "month" or "hours" etc as appropriate.

In PostgreSQL, you simply add and subtract interval values to datetime values:

'2001-06-27 14:43:21'::TIMESTAMP - '00:10:00'::INTERVAL = '2001-06-27 14:33:21'::TIMESTAMP
'2001-06-27 14:43:21'::TIMESTAMP- '2001-06-27 14:33:21'::TIMESTAMP = '00:10:00'::INTERVAL

For more information, see "Functions and Operators" in the PostgreSQL online docs.

To compute the first day of the month of a date: date_trunc('month', date)

First day of the next month: date_trunc('month', date) + '1 month'::INTERVAL

Add three months to the first day of the month of this date: date_trunc('month', date) + 3*('1 month'::INTERVAL)

The interval is a data type, not a string, and you can do computations with its values.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top