如何在PostgreSQL查询中将整数转换为字符串?
问题描述:
如何在PostgreSQL查询中将整数转换为字符串?
How do I convert an integer to string as part of a PostgreSQL query?
因此,例如,我需要:
SELECT * FROM table WHERE <some integer> = 'string of numbers'
其中<某个整数>
的长度可以是1到15位数字。
where <some integer>
can be anywhere from 1 to 15 digits long.
答
因为该数字最多可以是15位数字,将强制转换为64位(8字节)整数。试试这个:
Because the number can be up to 15 digits, you'll meed to cast to an 64 bit (8-byte) integer. Try this:
SELECT * FROM table
WHERE myint = mytext::int8
::
强制转换运算符具有历史意义,但很方便。 Postgres还符合SQL标准语法
The ::
cast operator is historical but convenient. Postgres also conforms to the SQL standard syntax
myint = cast ( mytext as int8)
如果您有文字文本,则想与 int进行比较
,将 int
转换为文本:
SELECT * FROM table
WHERE myint::varchar(255) = mytext