【问题标题】:Select then parse using substring and cast gives different result from computationSelect then parse using substring and cast 给出不同的计算结果
【发布时间】:2015-01-13 21:52:44
【问题描述】:

我解析了一个字符串,我使用子字符串来获取最后 11 个我认为的字符数。我完成了那个,但是当我使用 cast 和 round 时,它给出的结果与我所做的手动计算不同。

这是我的查询

SELECT round(cast(SUBSTRING('351856040520298,241111;1R,141117003059,A,1420.4629N,12058.7028E,0.0,77,0.9,20000006;2R,141117003059,11,98.3,12.58,04.10,282098820.9', 123,11)as float)/3600, 0, 1)

这给了我 583 的结果。但是当我尝试使用下面的计算手动计算时

282098820.9 / 3600

结果是

78360.7835

我的查询有问题吗?

感谢您的帮助。

【问题讨论】:

  • SELECT cast(SUBSTRING('351856040520298,241111;1R,141117003059,A,1420.4629N,12058.7028E,0.0,77,0.9,20000006;2R,141117003059,11,98.3,12.58,04.10,282098820.9', 123,11)as float) 为您返回什么值?在我看来,它就像 2098820.9

标签: sql-server parsing casting substring rounding


【解决方案1】:

问题在于您的SUBSTRING。它只返回2098820.9 而不是282098820.9。尝试使用 RIGHT 提取最后 11 个字符。

SELECT round(cast(right('351856040520298,241111;1R,141117003059,A,1420.4629N,12058.7028E,0.0,77,0.9,20000006;2R,141117003059,11,98.3,12.58,04.10,282098820.9', 11)as float)/3600, 0, 1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    相关资源
    最近更新 更多