4.2.9. Type Casts
4.2.9. Type Casts
4.2.9. 数据类型转换
A type cast specifies a conversion from one data type to another. PostgreSQL accepts two equivalent
syntaxes for type casts:
类型转换意为将数据类型从一种转换为另一种。PostgreSQL有两种类型转换语法:
CAST ( expression AS type )
expression::type
The
CAST
syntax conforms to SQL; the syntax with
::
is historical PostgreSQL usage.
CAST语法符合SQL标准;而语法::是PostgreSQL的历史用法。
When a cast is applied to a value expression of a known type, it represents a run-time type conversion.
The cast will succeed only if a suitable type conversion operation has been defined. Notice that this
is subtly different from the use of casts with constants, as shown in Section 4.1.2.7. A cast applied
to an unadorned string literal represents the initial assignment of a type to a literal constant value,
and so it will succeed for any type (if the contents of the string literal are acceptable input syntax for
the data type).
当将强制类型转换应用于已知类型的值表达式时,它表示运行时类型转换。仅当定义了合适的类型转换行为后,强制转换才能成功。 注意,这与使用带有常量的强制类型转换有细微的区别,如
第4.1.2.7节
所示。应用于未经修饰的字符串文字的强制转换表示将类型初始分配给文字常量值,因此对于任何类型都将成功(如果字符串文字的内容对于数据类型而言是可接受的输入语法)。
An explicit type cast can usually be omitted if there is no ambiguity as to the type that a value expression
must produce (for example, when it is assigned to a table column); the system will automatically
apply a type cast in such cases. However, automatic casting is only done for casts that are marked “OK
to apply implicitly” in the system catalogs. Other casts must be invoked with explicit casting syntax.
This restriction is intended to prevent surprising conversions from being applied silently.
如果对值表达式产生的值数据类型没有歧义(例如,当将其分配给表列时),则通常可以省略显式类型转换,在这种情况下,系统会自动进行类型转换。但是,仅对系统目录中标记为“可以隐式转换”的转换进行自动强制转换。 其他强制转换必须使用显式强制转换语法来完成。此限制旨在防止意外地进行异常转换。
It is also possible to specify a type cast using a function-like syntax:
也可以使用类似函数的语法来指定类型转换:
typename ( expression )
However, this only works for types whose names are also valid as function names. For example,
double
precision
cannot be used this way, but the equivalent
float8
can. Also, the names
in
terval
,
time
, and
timestamp
can only be used in this fashion if they are double-quoted, because
of syntactic conflicts. Therefore, the use of the function-like cast syntax leads to inconsistencies and
should probably be avoided.
然而,此语法仅对于那些名称也可以用作函数名称的数据类型。例如,double precision就不能这样使用,但是float8就可以。而且,如果interval,time,timestamp用双引号引起来,由于语法冲突,那么也只能这么使用。因此,使用类似函数的强制类型转换语法会导致不一致,所以,需要尽量避免。
Note
注
The function-like syntax is in fact just a function call. When one of the two standard
cast syntaxes is used to do a run-time conversion, it will internally invoke a registered
function to perform the conversion. By convention, these conversion functions have
the same name as their output type, and thus the “function-like syntax” is nothing more
than a direct invocation of the underlying conversion function. Obviously, this is not
something that a portable application should rely on. For further details see CREATE
CAST.
类似函数的语法实际上只是对于函数的调用。
当使用两种标准强制转换语法之一进行运行时转换时,它将在内部调用注册的函数来执行转换。按照惯例,这些转换函数与其输出类型具有相同的名称,因此“类似于函数的语法”无非就是直接调用基础转换函数。显然,这不是可移植应用程序应依赖的机制。有关更多详细信息,请参见
CREATE CAST
。