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.
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.
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.
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
Sí
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;
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.
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
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;
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))...
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