如何将Datemange传递给Postgres功能?

发布于 2025-01-24 17:26:55 字数 418 浏览 2 评论 0 原文

我需要以格式[2018-01-01,2018-2-28] 的格式传递

杂物

create or replace function extract_range(outer_range daterange, inner_range daterange);

2

select extract_range(['2018-01-01', '2018-12-31'], ['2018-03-01', '2018-3-31']);

个 通话中的错误:

语法错误或附近“” [“”

正确的调用是什么?

还是我应该将它们作为字符串传递,然后在功能中解析/转换/铸造?

Postgres documentation says there's a built-in daterange type but it doesn't seem to work when I write a function in DBeaver. It's not highlighted or recognized as such.

I need to pass 2 dateranges in the format of [2018-01-01, 2018-2-28] to a Postgres function for range extraction, etc.

Declaration:

create or replace function extract_range(outer_range daterange, inner_range daterange);

Call:

select extract_range(['2018-01-01', '2018-12-31'], ['2018-03-01', '2018-3-31']);

The function compiles but throws an error at the call:

syntax error at or near "["

What's the correct call?

Or should I just pass them as a string, and then parse/convert/cast inside the function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

女中豪杰 2025-01-31 17:26:55

paterange 您必须使用:

daterange('2018-01-01', '2018-12-31', '[]') 

第三个参数定义边缘是否包含或独家。您没有指定您想要的东西。

所以你需要:

select extract_range(daterange('2018-01-01', '2018-12-31', '[]'),
                     daterange('2018-03-01', '2018-03-31', '[]');

As documented in the manual the syntax to create a range is to use the range type name as a "creator" function, e.g. to create a daterange you have to use:

daterange('2018-01-01', '2018-12-31', '[]') 

The third parameter defines if the edges are inclusive or exclusive. You didn't specify what you want there.

So you need:

select extract_range(daterange('2018-01-01', '2018-12-31', '[]'),
                     daterange('2018-03-01', '2018-03-31', '[]');
鯉魚旗 2025-01-31 17:26:55

a_horse已经提供了函数调用的适当语法

或者,通过

SELECT extract_range('[2018-01-01,2019-01-01)'
                   , '[2018-03-01,2019-03-31)');

如果您不确定语法,请询问Postgres(示例为PSQL):

test=> SELECT daterange('2018-01-01', '2018-12-31', '[]');
        daterange        
-------------------------
 [2018-01-01,2019-01-01)
(1 row)

要明确,您可以添加显式类型的铸件:

SELECT extract_range('[2018-01-01,2019-01-01)'::daterange
                   , '[2018-03-01,2019-04-01)'::daterange);

但是,这是可选的,而函数类型分辨率不会符合含糊不清的情况。功能。

请注意,默认 depaterange 值包括下部( [),并排除上限()。

是的,通常将基本类型传递给Postgres功能并构造内部范围更简单。

无论哪种方式,都要清楚要包含哪些范围。

a_horse already provided proper syntax for the function call.

Alternatively, pass literal constants. That should work with every client:

SELECT extract_range('[2018-01-01,2019-01-01)'
                   , '[2018-03-01,2019-03-31)');

If you are unsure about the syntax, just ask Postgres (example with psql):

test=> SELECT daterange('2018-01-01', '2018-12-31', '[]');
        daterange        
-------------------------
 [2018-01-01,2019-01-01)
(1 row)

To be absolutely clear, you can add explicit type casts:

SELECT extract_range('[2018-01-01,2019-01-01)'::daterange
                   , '[2018-03-01,2019-04-01)'::daterange);

But that's optional while function type resolution does not run into ambiguities with overloaded functions.

Note that a default daterange value includes the lower ([) and excludes the upper bound ()).

And yes, it's typically simpler to just pass basic types to the Postgres function and construct ranges inside.

Either way, be clear about which bounds to include.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文