Cast int to varchar


Tengo la siguiente consulta y necesito convertir id a varchar

Esquema

create table t9 (id int, name varchar (55));
insert into t9( id, name)values(2, 'bob');

Lo que he intentado

select CAST(id as VARCHAR(50)) as col1 from t9;

select CONVERT(VARCHAR(50),id) as colI1 from t9;

Pero no funcionan. Por favor sugiera.

Author: Mr. Polywhirl, 2013-03-12

7 answers

Usted tendrá que cast o convert como un tipo de datos CHAR, no hay un tipo de datos varchar al que pueda convertir/convertir datos:

select CAST(id as CHAR(50)) as col1 
from t9;

select CONVERT(id, CHAR(50)) as colI1 
from t9;

Vea el siguiente SQL-in action-over en SQL Fiddle :

/*! Build Schema */
create table t9 (id INT, name VARCHAR(55));
insert into t9 (id, name) values (2, 'bob');

/*! SQL Queries */
select CAST(id as CHAR(50)) as col1 from t9;
select CONVERT(id, CHAR(50)) as colI1 from t9;

Además del hecho de que estaba tratando de convertir a un tipo de datos incorrecto, la sintaxis que estaba utilizando para convert era incorrecta. La función convert utiliza lo siguiente donde expr es su columna o valor:

 CONVERT(expr,type)

O

 CONVERT(expr USING transcoding_name)

Su consulta original tenía la sintaxis al revés.

 170
Author: Taryn,
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-12-21 16:00:30

Lo estás obteniendo porque "VARCHAR" no es un tipo válido para ser lanzado. De acuerdo con los documentos de MySQL ( http://dev.mysql.com/doc/refman/5.5/en/cast-functions.html#function_cast ) solo se puede lanzar a:

  • BINARIO[(N)]
  • CHAR [(N)]
  • FECHA
  • DATETIME
  • DECIMAL [(M [, D])]
  • FIRMADO
  • [ENTERO]
  • TIEMPO
  • UNSIGNED [INTEGER]

Creo que lo mejor es usar CHAR.

 29
Author: Aaron,
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
2013-03-12 18:08:42

SELECT id || '' FROM some_table;
or SELECT id::text FROM some_table;

Es postgresql, ¡pero MySQL no lo permite!

Atajo en MySQL:

SELECT concat(id, '') FROM some_table;
 11
Author: nancy,
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
2015-09-12 18:19:43

No tengo MySQL, pero hay RDBMS (Postgres, entre otros) en los que puedes usar el hack

SELECT id || '' FROM some_table;

El concatenado hace una conversión implícita.

 3
Author: Andrew Lazarus,
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
2013-03-13 06:24:22

Resolví un problema al comparar una columna entera x una columna varchar con

where CAST(Column_name AS CHAR CHARACTER SET latin1 ) collate latin1_general_ci = varchar_column_name

 2
Author: user6348455,
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-03-16 21:54:03

Uso:

SELECT cast(CHAR(50),id) as colI1 from t9;
 0
Author: user2132046,
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
2013-03-12 18:08:28

Voy a responder a esto en términos generales, y muy agradecido a los contribuidores anteriores.
Estoy usando MySQL en MySQL Workbench. Tuve un problema similar al intentar concatenar un char y un int juntos usando el método GROUP_CONCAT. En resumen, lo que ha funcionado para mí es esto:

digamos que su char es ' c 'y int es 'i', por lo tanto, la consulta se convierte en:
...GROUP_CONCAT(CONCAT(c,' ', CAST(i AS CHAR))...

 0
Author: H.B,
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-03-16 21:01:14