我需要以格式[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?
发布评论
评论(2)
paterange 您必须使用:
第三个参数定义边缘是否包含或独家。您没有指定您想要的东西。
所以你需要:
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:The third parameter defines if the edges are inclusive or exclusive. You didn't specify what you want there.
So you need:
a_horse已经提供了函数调用的适当语法。
或者,通过
如果您不确定语法,请询问Postgres(示例为PSQL):
要明确,您可以添加显式类型的铸件:
但是,这是可选的,而函数类型分辨率不会符合含糊不清的情况。功能。
请注意,默认
depaterange
值包括下部([
),并排除上限()
)。是的,通常将基本类型传递给Postgres功能并构造内部范围更简单。
无论哪种方式,都要清楚要包含哪些范围。
a_horse already provided proper syntax for the function call.
Alternatively, pass literal constants. That should work with every client:
If you are unsure about the syntax, just ask Postgres (example with psql):
To be absolutely clear, you can add explicit type casts:
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.