c# - 如何选择 Distinct char 数据类型并通过使用 sql 强制转换为 int

标签 c# sql-server visual-studio

我已将 Company_Code 保存在 Company 表中,作为 char 数据类型。现在,我想将 Company_Code 作为 integer 数据类型检索为 DISTINCT (没有值应该重复)并以 的形式>整数数组。

我正在使用这个查询:

SELECT DISTINCT Company_Code 
FROM Company  
ORDER BY CAST(Company_Code AS INT);

此代码显示以下错误:

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

如果可能,请告诉我?

或者如果我做错了什么?

最佳答案

您可以将 DISTINCTGROUP BY 交换:

SELECT Company_Code 
FROM Company 
GROUP BY Company_Code 
ORDER By Cast(Company_Code as int);
         -- if it is possible that CAST will fail, then you should use TRY_CAST

Rextester Demo

更多信息:How SQL DISTINCT and ORDER BY are Related

关于c# - 如何选择 Distinct char 数据类型并通过使用 sql 强制转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51901835/

相关文章:

visual-studio - 从 Visual Studio 中对 Artifactory 上的 NuGet 进行身份验证

c# - 分配给委托(delegate)的通用方法

c# - Crystal Reports MVVM 数据源

c# - XML 文档 SelectSingleNode 返回 null

sql-server - SQL中的服务评级总结

c# - Visual Studio 2013 中的 "New Project"对话框中缺少项目模板

c# - 在所有者类中使用属​​性与支持字段

sql-server - 删除记录时出现 NO_SQL_DATA 错误,firedac,delphi 10.3.1

sql - 返回数据,即使它们是错误的

javascript - 为什么我的 Intellisense 消失在 Visual Studio 中的 Javascript 文件中?