¿Cómo convierto un entero a cadena como parte de una consulta PostgreSQL?


¿Cómo convertir un entero a cadena como parte de una consulta PostgreSQL?

Así que, por ejemplo, necesito:

SELECT * FROM table WHERE <some integer> = 'string of numbers'

Donde <some integer> puede tener entre 1 y 15 dígitos.

Author: Eric Leschinski, 2012-12-11

3 answers

Debido a que el número puede ser de hasta 15 dígitos, usted meed para convertir a un entero de 64 bits (8 bytes). Prueba esto:

SELECT * FROM table
WHERE myint = mytext::int8

El operador de reparto :: es histórico pero conveniente. Postgres también se ajusta a la sintaxis estándar SQL

myint = cast ( mytext as int8)
 66
Author: Bohemian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-10 21:39:41

Puede convertir un entero a una cadena de esta manera

intval::text

Y así, en su caso

SELECT * FROM table WHERE <some integer>::text = 'string of numbers'
 62
Author: Brugolo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-10 10:53:36

Puedes hacer esto:

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

 1
Author: djgupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-09 14:35:56