How do I convert an integer to string as part of a

2019-02-01 20:27发布

How do I convert an integer to string as part of a PostgreSQL query?

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.

3条回答
贪生不怕死
2楼-- · 2019-02-01 20:41

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'
查看更多
Melony?
3楼-- · 2019-02-01 20:54

You could do this:

SELECT * FROM table WHERE cast(YOUR_INTEGER_VALUE as varchar) = 'string of numbers'

查看更多
不美不萌又怎样
4楼-- · 2019-02-01 20:55

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

The :: cast operator is historical but convenient. Postgres also conforms to the SQL standard syntax

myint = cast ( mytext as int8)
查看更多
登录 后发表回答