作为 PostgreSQL 查询的一部分,如何将整数转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13809547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert an integer to string as part of a PostgreSQL query?
提问by spyd3rr
How do I convert an integer to string as part of a PostgreSQL query?
作为 PostgreSQL 查询的一部分,如何将整数转换为字符串?
So, for example, I need:
因此,例如,我需要:
SELECT * FROM table WHERE <some integer> = 'string of numbers'
where <some integer>
can be anywhere from 1 to 15 digits long.
where<some integer>
可以是 1 到 15 位数字。
回答by Brugolo
You can cast an integer to a string in this way
您可以通过这种方式将整数转换为字符串
intval::text
and so in your case
所以在你的情况下
SELECT * FROM table WHERE <some integer>::text = 'string of numbers'
回答by Bohemian
Because the number can be up to 15 digits, you'll meed to cast to an 64 bit (8-byte) integer. Try this:
由于该数字最多可以有 15 位数字,因此您需要强制转换为 64 位(8 字节)整数。尝试这个:
SELECT * FROM table
WHERE myint = mytext::int8
The ::
cast operator is historical but convenient. Postgres also conforms to the SQL standard syntax
该::
转换运算符是历史的,但方便。Postgres 也符合 SQL 标准语法
myint = cast ( mytext as int8)
If you have literal text you want to compare with an int
, cast the int
to text:
如果您有想要与 an 进行比较的文字文本,请将int
其转换int
为文本:
SELECT * FROM table
WHERE myint::varchar(255) = mytext
回答by djgupta
You could do this:
你可以这样做:
SELECT * FROM table WHERE cast(YOUR_INTEGER_VALUE as varchar) = 'string of numbers'