MySQL 类型转换

1.问题描述

  在项目中遇到要将Int类型转为Char类型,然后利用转化后的Char类型进行模糊查询。

  例:合同编号在数据库中为int类型 8066

    用利用 806 模糊查询出合同编号为8066数据记录

2. 解决方案

  利用MySQL中两个函数其中之一,将合同编号转为Char类型,然后利用模糊查询查出记录:

   CAST(expr AS type)
   CONVERT(expr,type)

3. 例:

  CAST

  select
  *
  from 
  t_order_delivery_noticedetails
  where
  CAST(htbh as char(10)) like '%806%'

   CONVERT

  select
  *
  from 
  t_order_delivery_noticedetails
  where
  CONVERT(htbh,char(10)) like '%806%'

  

  利用上诉转化可以实现需求,同时这两个函数支持其他类型的转化。

参考资料

  1. https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_cast

  2. http://stackoverflow.com/questions/15368753/cast-int-to-varchar

原文地址:https://www.cnblogs.com/springlight/p/6373391.html