Postgres数据类型转换 [英] Postgres data type cast
问题描述
我的数据库是Postgres 8.我需要转换数据类型到另一个。这意味着,列数据类型之一是 varchar
,并需要将其转换为 int
与Postgres在 SELECT
语句。
My database is Postgres 8. I need to cast data type to another. That means, one of columns data type is varchar
and need to cast it into int
with Postgres in a SELECT
statement.
目前,我获得字符串值并将其转换为 int
在Java。
有什么办法吗?示例代码将非常感谢。
Currently, I get the string value and cast it into int
in Java.
Is there any way to do it? Sample code would be highly appreciated.
推荐答案
SELECT varchar_col::int FROM tbl;
或
cast(varchar_col AS int) -- SQL standard
这些语法变体在任何地方都有效。还有两个:
Theses syntax variants are valid anywhere. There are two more:
int4(varchar_col) -- only works for some type names
int '123' -- must be an untyped simple literal constant
注意我如何写 int4
(varchar_col)。这是内部类型名称,还有一个为它定义的函数。不能作为 或 integer()
int()
Note how I wrote int4
(varchar_col). That's the internal type name and there is also a function defined for it. Wouldn't work as or integer()
.int()
Details in the manual here and here.
适用于 integer
,字符串必须包含可选的前导符号(
+
/ -
)只要。前导/尾随空白被忽略。
To be valid for integer
, the string must be comprised of an optional leading sign (+
/-
) followed by digits only. Leading / trailing white space is ignored.
这篇关于Postgres数据类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!