生成一系列日期-使用日期类型作为输入 [英] Generate series of dates - using date type as input
问题描述
的 文档说,该参数可以是
感谢 函数类型解析 ,我们还可以将 对于裸露的 Postgres基本上区分了三种类型的演员:>
必须在系统中从输入类型向期望类型注册一个隐式强制转换,以使函数以静默方式接受(并转换)输入值. 要查看为 What is the reason that
Thanks to function type resolution we can also pass For a bare Postgres basically distinguishes between three types of casts: There has to be an implicit cast registered in the system from the input type to the expected type to make a function silently accept (and convert) an input value. To see which casts are defined to All of these casts are implicit. Per documentation on Indicates what contexts the cast can be invoked in. Bold emphasis mine. 这篇关于生成一系列日期-使用日期类型作为输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!int
或bigint
用于generate_series(start, stop)
和generate_series(start, stop, step)
情况,timestamp
或timestamp with time zone
用于generate_series(start, stop, step interval)
.generate_series
也可以使用date
类型作为输入并返回timestamp with timezone
的原因是什么?pg=# select generate_series('2014-01-01'::date,'2014-01-02'::date,'1 day');
generate_series
------------------------
2014-01-01 00:00:00+01
2014-01-02 00:00:00+01
(2 rows)
date
值传递给 generate_series()
,因为从date
到timestamp
以及从date
到timestamptz
都有一个隐式转换.会模棱两可,但是timestamptz
在日期/时间类型"中是首选" .详细说明:date
,本地时间00:00
被假定为演员表.请注意,如果使用date
作为输入,当前时区设置将直接影响结果,因为很明显,"2014-01-10 00:00"在Tokio中表示的时间与在纽约时不同. /p>
Postgres如何确定可接受的类型?
Explicit casts
..使用CAST
或::
语法时.
Assignment cast
..在将值分配给目标列时隐式转换.
Implicit cast
..所有其他表达式中的隐式强制转换.timestamptz
定义了哪些强制类型转换,可以查询目录表Documentation for generate_series
says that argument can be int
or bigint
for generate_series(start, stop)
and generate_series(start, stop, step)
cases and timestamp
or timestamp with time zone
for generate_series(start, stop, step interval)
.generate_series
works also with date
type as input and returns timestamp with timezone
?pg=# select generate_series('2014-01-01'::date,'2014-01-02'::date,'1 day');
generate_series
------------------------
2014-01-01 00:00:00+01
2014-01-02 00:00:00+01
(2 rows)
date
values to generate_series()
because there is an implicit cast from date
to timestamp
as well as from date
to timestamptz
. Would be ambiguous, but timestamptz
is "preferred" among "Date/time types". Detailed explanation:date
the local time 00:00
is assumed in the cast. Be aware that the current time zone setting directly affects the result if you use date
as input since, obviously, '2014-01-10 00:00' represents a different point in time in Tokio than it does in New York.How does Postgres decide what types are acceptable?
Explicit casts
.. when using CAST
or ::
syntax.
Assignment cast
.. implicit cast when a value is assigned to a target column.
Implicit cast
.. implicit casts in all other expressions.timestamptz
, you can query the catalog table pg_cast
:SELECT castsource::regtype, casttarget::regtype, castcontext
FROM pg_cast
WHERE casttarget = 'timestamptz'::regtype;
castsource | casttarget | castcontext
-----------------------------+--------------------------+-------------
abstime | timestamp with time zone | i
date | timestamp with time zone | i
timestamp without time zone | timestamp with time zone | i
timestamp with time zone | timestamp with time zone | i
castcontext
:
e
means only as an
explicit cast (using CAST
or ::
syntax). a
means implicitly in
assignment to a target column, as well as explicitly. i
means
implicitly in expressions, as well as the other cases.