zoukankan      html  css  js  c++  java
  • mysql中将一个数据类型转换成另外的数据类型?mysql中cast函数的使用?

    需求描述:

      今天在看mysql的函数,提到了通过cast函数将一个数据类型值转换为特定类型的结果值.

      在此记录下.将一个表达式转换为特定精度的小数.

    操作过程:

    1.查看6/4的结果

    mysql> select 6/4;
    +--------+
    | 6/4    |
    +--------+
    | 1.5000 |
    +--------+
    1 row in set (0.00 sec)

     备注:通过查询结果可以知道,结果是精确到小数点后4位的.

    2.通过cast函数,将这个结果转换为decimal数据类型,并且指定精确到小数点后1位

    mysql> select cast(6/4 as decimal(3,1));
    +---------------------------+
    | cast(6/4 as decimal(3,1)) |
    +---------------------------+
    |                       1.5 |
    +---------------------------+
    1 row in set (0.00 sec)

    备注:通过结果可以知道,目前呢,已经转为decimal数据类型.

    另:decimal(3,1)表示最多3位数字,精确到小数点后1位,执行四舍五入.

    官方文档参考:

    CAST(expr AS type)
    
    The CAST() function takes an expression of any type and produces a result value of the specified type, similar to CONVERT().
    For more information, see the description of CONVERT(). CAST() is standard SQL syntax.

    文档创建时间:2018年6月26日15:38:27

  • 相关阅读:
    7种思维
    微服务架构
    最近面试被问到一个问题,AtomicInteger如何保证线程安全?
    Socket netty ...
    Spring-Boot配置文件web性能(服务器)配置项
    P2P互联网金融企业的四大转型方向
    分布式,微服务 区别联系 理解.
    几个好问题
    netty
    结构化思维
  • 原文地址:https://www.cnblogs.com/chuanzhang053/p/9229497.html
Copyright © 2011-2022 走看看