如何在PostgreSQL查询中将整数转换为字符串? [英] How do I convert an integer to string as part of a PostgreSQL query?

查看:248
本文介绍了如何在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

这篇关于如何在PostgreSQL查询中将整数转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆