development

int를 varchar로 캐스트

big-blog 2020. 8. 3. 17:19
반응형

int를 varchar로 캐스트


아래 쿼리가 있고 전송해야 id합니다.varchar

개요

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

내가 시도한 것

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

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

그러나 그들은 작동하지 않습니다. 제안 해주세요.


당신이 필요합니다 cast또는 convertA와 CHAR데이터 유형에는 없다 varchar당신이 / 변환 데이터를 캐스팅 할 수있는 데이터 형식 :

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

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

SQL Fiddle 에서 다음 SQL (실제로)을 확인하십시오 .

/*! 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;

잘못된 데이터 유형으로 변환하려고했지만 사용중인 구문 convert이 잘못되었습니다. convert함수는 expr열 또는 값이 있는 곳을 다음과 같이 사용합니다 .

 CONVERT(expr,type)

또는

 CONVERT(expr USING transcoding_name)

원래 쿼리의 구문이 거꾸로되었습니다.


VARCHAR올바른 유형이 아니기 때문에 얻을 수 있습니다. MySQL 문서 ( http://dev.mysql.com/doc/refman/5.5/en/cast-functions.html#function_cast ) 에 따르면 다음으로 만 캐스트 할 수 있습니다.

  • 이진 [(N)]
  • CHAR [(N)]
  • 데이트
  • 날짜 시간
  • DECIMAL [(M [, D])]
  • 서명
  • [정수]
  • 시각
  • 서명되지 않은 [INTEGER]

최선의 방법은을 사용하는 것 CHAR입니다.


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

postgresql이지만 mySql은 허용하지 않습니다!

mySql에서 바로 가기 :

SELECT concat(id, '') FROM some_table;

나는 MySQL이 없지만 해킹을 사용할 수있는 RDBMS (Postgres 등)가 있습니다.

SELECT id || '' FROM some_table;

연결은 암시 적 변환을 수행합니다.


정수 열 xa varchar열과 비교하는 문제를 해결했습니다.

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


사용하다 :

SELECT cast(CHAR(50),id) as colI1 from t9;

I will be answering this in general terms, and very thankful to the above contributers.
I am using MySQL on MySQL Workbench. I had a similar issue trying to concatenate a char and an int together using the GROUP_CONCAT method. In summary, what has worked for me is this:

let's say your char is 'c' and int is 'i', so, the query becomes:
...GROUP_CONCAT(CONCAT(c,' ', CAST(i AS CHAR))...

참고URL : https://stackoverflow.com/questions/15368753/cast-int-to-varchar

반응형