【问题标题】:MySQL Cast to Float or What Decimal valueMySQL Cast to Float 或什么十进制值
【发布时间】:2014-05-31 01:59:15
【问题描述】:

我为保险环境开发软件,因此十进制值非常重要。 我们正在从 MSSQL 转换为 MYSQL。我的问题涉及的查询是:

SELECT CAST(
    CAST(
      (initialpremium - totalpremium) / 2 * 
      CAST(premiummultiplier AS INT) 
    AS FLOAT) 
FROM policy 
/* rest of the query... */

此代码在 MSSQL 中有效,但在 MYSQL 中无效。 This is valid cast types for mysql

我的问题是,浮点数的正确十进制长度是多少。 This link does not help. But describes converting from float to real in MSSQL 和我在网上搜索了float datasize mssql

【问题讨论】:

  • 我找到了答案。而不是在 mysql 中指定十进制值,您只需将代码更改为 cast(value as DECIMAL)

标签: mysql sql-server type-conversion floating-point-precision


【解决方案1】:

MySQL 可以通过添加0.0 将整数转换为浮点数:

SELECT
    integer_column + 0.0 AS float_column

【讨论】:

    【解决方案2】:

    我找到了答案。而不是在 mysql 中指定十进制值,您只需将代码更改为 cast(value as DECIMAL)

    【讨论】:

      【解决方案3】:
      select integer_column::float from tablename;
      

      【讨论】:

      • ::float 是 PostgreSQL 中的一个简写形式。 OP的问题是关于MySQL的,它没有。
      猜你喜欢
      • 2022-09-29
      • 2021-04-16
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多