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

How to convert an integer to a 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 between 1 and 15 digits long.

+76
postgresql
Dec 10
source share
3 answers

Since a number can be up to 15 digits, you need to cast to a 64-bit (8-byte) integer. Try this:

 SELECT * FROM table WHERE myint = mytext::int8 

The :: cast operator is historical, but convenient. Postgres also follows standard SQL syntax

 myint = cast ( mytext as int8) 



If you have literal text that you want to compare with int , cast int to text:

 SELECT * FROM table WHERE myint::varchar(255) = mytext 
+78
Dec 10 '12 at 21:32
source share

This way you can highlight an integer in a string

 intval::text 

and therefore in your case

 SELECT * FROM table WHERE <some integer>::text = 'string of numbers' 
+94
Feb 10 '16 at 10:53 on
source share

You could do this:

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

+3
Aug 09 '18 at 14:35
source share



All Articles