mysql int转varchar的 cast,convert 函数
原创 sorryboy 发表于:2018-06-29 16:21:54
  阅读 :268   收藏   编辑

在mysql中可以使用cast,convert做类型转换

语法

CAST(value as type);
CONVERT(value, type);

但是类型是有限制的,不可转varchar 类型,可转换类型如下:

  • 二进制,同带binary前缀的效果 : BINARY    

  • 字符型,可带参数 : CHAR()     

  • 日期 : DATE     

  • 时间: TIME     

  • 日期时间型 : DATETIME     

  • 浮点数 : DECIMAL      

  • 整数 : SIGNED     

  • 无符号整数 : UNSIGNED 

样例

create table test (id int, name varchar(30));
insert into test (id, name) values (1, 'zhangSan');

查询时类型转换

select cast(id as char(8)) as id  from test;
select convert(id,char(10)) as id from test;

在hibernate中使用createSqlQuery对于char类型数据精度可以使用该函数