【问题标题】:Sum Function and Changing the type of VariableSum 函数和改变变量的类型
【发布时间】:2013-03-22 07:59:07
【问题描述】:

我正在为我的项目使用 SQL Server Express。我有一张像这样的桌子:

Number     Name     Surname     Point     Position
------     ----     -------     -----     --------
1          John     Black       10000     True
2          Jane     Lincoln     8800      True
3          Edward   Payne       17000     False
ETC...

我想准备一个查询,将 Point 求和,其中位置为真

SELECT Sum(Point) AS Exp1
FROM   DataTable
WHERE  Position = True

我的问题是Position 的类型是Nvarchar。所以查询不会对 Point 求和。我尝试将类型 Nvarchar 更改为 int,但我有一个大项目,它给出了几个错误。有什么方法可以查询以求和 Point 吗?

(我尝试使用Sum(Var(Point)),但不起作用)

【问题讨论】:

  • Bad habits to kick : choosing the wrong data type - 你应该始终使用最合适的数据类型 - 毕竟这就是它们的用途!因此,与其现在应用另一个创可贴来解决问题 - 您应该花时间调查为什么在尝试将此列转换为 INT 时出现错误,并修复这些问题,以便您可以将此列转换为适当的数据类型!

标签: sql-server vb.net visual-studio-2010 vba


【解决方案1】:

使用

select sum(CAST(Point AS INT)) as Exp1 from DataTable where position = 'True'

 select sum(CONVERT(int, Point)) as Exp1 from DataTable where position = 'True'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2015-11-11
    • 2016-01-24
    相关资源
    最近更新 更多