如何将整数转换为字符串作为PostgreSQL查询的一部分?
例如,我需要:
SELECT * FROM table WHERE <some integer> = 'string of numbers'
<某个整数>可以是1到15位的任意数字。
如何将整数转换为字符串作为PostgreSQL查询的一部分?
例如,我需要:
SELECT * FROM table WHERE <some integer> = 'string of numbers'
<某个整数>可以是1到15位的任意数字。
因为这个数字最多可以是15位,所以需要转换为64位(8字节)整数。试试这个:
SELECT * FROM table
WHERE myint = mytext::int8
:: cast操作符具有历史意义,但很方便。Postgres也符合SQL标准语法
myint = cast ( mytext as int8)
如果你有文字文本,想要与int型比较,将int型转换为text:
SELECT * FROM table
WHERE myint::varchar(255) = mytext
可以用这种方法将整数强制转换为字符串
intval::text
所以在你的情况下
SELECT * FROM table WHERE <some integer>::text = 'string of numbers'
你可以这样做:
SELECT * FROM table WHERE cast(YOUR_INTEGER_VALUE as varchar) = 'string of numbers'
如果一些可以存储为字符串的整数包含小数点,并且您想要比较十进制和十进制,下面将会有所帮助
select NULLIF('105.0', '')::decimal
SELECT * FROM table WHERE NULLIF('105.0', '')::decimal = 105.0
以下不会转换
select NULLIF('105.0', '')::int
select NULLIF('105.0', '')::integer
对于这个问题,你只要跳过就行了
select 105.3::text