【问题标题】:Convert number to string in n1ql query output from Couchbase DB在 Couchbase DB 的 n1ql 查询输出中将数字转换为字符串
【发布时间】:2017-07-13 13:33:37
【问题描述】:

我需要以下值作为 Couchbase DB 的 N1QL 查询输出中的字符串。

当前 -

"cas": 1499937943452123136

预期 -

"cas": "1499937943452123136"

Cbq 查询:

select meta().cas from sampledb where type = "PROD" and Id = 
"PROD_101"

请求您的帮助。

【问题讨论】:

    标签: couchbase tostring data-conversion n1ql


    【解决方案1】:

    使用 to_string 函数。

    select to_string(meta().cas) from sampledb where type = "PROD" and Id = "PROD_101"
    

    您使用的是哪个版本的 Couchbase?

    【讨论】:

    • 嗨@kbinstance,结果现在是一个字符串,但它已将最后两位数字四舍五入。输出为“cas”:“1499937943452123100”。但我需要的值为 "cas": "1499937943452123136"
    • 当 TO_STRING 参数超过某些值并分配给浮点数时,它会丢失精度。这已在功能版本中修复。
    • @Emraan :正如 vsr 所说,这是一个已知问题,已在即将发布的 5.0 beta 版本中修复。
    【解决方案2】:

    要将字符串转换为数字,请使用 to_string() 方法,如下例所示

    如果要将字符串转换为数字,请使用 TONUMBER() 方法,如下例所示

    查询:

    select sum(TONUMBER(installmentPremium)) TotalInstallmentPremium  from data
    where installmentPremium > "10" and type_="ProductComponentOption" limit 100;
    

    结果:

    [
      {
        "TotalInstallmentPremium": 15757.470000000001
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多