数字不按顺序排序
numbers not sorting in order
我正在尝试使用 asp.net 和 c# 在 sql 中按顺序(从 1 到 ...)对列进行排序。在做了一些研究之后,我似乎需要将 SQL 服务器中的列类型更改为 int 但是这是不可能的,因为该列正在存储门牌号,我最终可能会得到门牌号 10a(例如)所以当前设置为varchar。因此,它不能正确地对列进行排序。我已经尝试将相关列转换为 int,但是在运行应用程序时,我在 gridview 中遇到了它的绑定错误,我也尝试了其他的 * 1,但在运行 [=15 时仍然在绑定部分出现错误=]
SQL声明
SELECT DISTINCT tblcontact.ContactID, tblcontact.Forename, tblcontact.Surname,
tbladdress.[House Number], tbladdress.AddressLine1, tbladdress.AddressLine2,
tblcontact.[Business Name] FROM tblcontact INNER JOIN tbladdress ON tblcontact.AddressID = tbladdress.AddressID
LEFT OUTER JOIN tblDonate
ON tblcontact.ContactID = tblDonate.ContactID
WHERE (tbladdress.CollectionArea = @CollectionArea) AND
(tbladdress.AddressLine1 = @drpCollectionStreet)
ORDER BY tbladdress.[House Number] ASC
网格视图标记
<asp:TemplateField HeaderText="House Number">
<EditItemTemplate>
<asp:TextBox ID="txtHouseNum" runat="server" Text='<%# Bind("[House Number]") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblHouseNum" runat="server" Text='<%# Bind("[House Number]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
这是一个如何排序的例子
21
22
27
28
5
6
8
9
这就是我转换为 int 的方式,它在 SQL 中排序良好,但我在 Bind 上遇到错误(上图)
SELECT DISTINCT tblcontact.ContactID, tblcontact.Forename, tblcontact.Surname, cast(tbladdress.[House Number] as int),
tbladdress.AddressLine1, tbladdress.AddressLine2, tblcontact.[Business Name]
FROM tblcontact INNER JOIN tbladdress ON tblcontact.AddressID = tbladdress.AddressID
LEFT OUTER JOIN tblDonate ON tblcontact.ContactID = tblDonate.ContactID
WHERE (tbladdress.CollectionArea = 'Queens Park') AND (tbladdress.AddressLine1 = 'Kings Road')
ORDER BY cast(tbladdress.[House Number] as int)
运行时出错
这是运行时的错误(这个最有意义)
"House Number is neither a DataColumn nor a DataRelation for table DefaultView."}
要按数字对街道号码进行排序,您需要将它们转换为数字。当然,麻烦在于像“10a”这样的字符串不能转换为 INT。答案是从 [House Number] 列中提取数字字符,然后进行转换。以下为 ORDER BY 子句执行此操作:
...
ORDER BY CAST(SUBSTRING(tbladdress.[House Number], PATINDEX('%[0-9]%', tbladdress.[House Number]),
1 + PATINDEX('%[0-9][^0-9]%', tbladdress.[House Number] + ' ') -
PATINDEX('%[0-9]%',tbladdress.[House Number])) AS INT)
请参阅 http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/extracting-numbers-with-sql-server/ 了解复杂公式的工作原理。
从 select column list
中删除 cast
。仅保留 order by
仍然会排序结果。
select A.ContactID,
A.Forename,
A.Surname,
A.[House Number],
A.AddressLine1,
A.AddressLine2,
A.[Business Name] from (
SELECT DISTINCT tblcontact.ContactID,
tblcontact.Forename,
tblcontact.Surname,
tbladdress.[House Number],
tbladdress.AddressLine1,
tbladdress.AddressLine2,
tblcontact.[Business Name]
FROM tblcontact
INNER JOIN tbladdress
ON tblcontact.AddressID = tbladdress.AddressID
LEFT OUTER JOIN tblDonate
ON tblcontact.ContactID = tblDonate.ContactID
WHERE ( tbladdress.CollectionArea = 'Queens Park' )
AND ( tbladdress.AddressLine1 = 'Kings Road' )) A
ORDER BY Cast(tbladdress.[House Number] AS INT)
正确答案是 JohnS 和 NoDisplayName 答案的组合,谢谢你们
select A.ContactID,
A.Forename,
A.Surname,
A.[House Number],
A.AddressLine1,
A.AddressLine2,
A.[Business Name] from (
SELECT DISTINCT tblcontact.ContactID,
tblcontact.Forename,
tblcontact.Surname,
tbladdress.[House Number],
tbladdress.AddressLine1,
tbladdress.AddressLine2,
tblcontact.[Business Name]
FROM tblcontact
INNER JOIN tbladdress
ON tblcontact.AddressID = tbladdress.AddressID
LEFT OUTER JOIN tblDonate
ON tblcontact.ContactID = tblDonate.ContactID
WHERE ( tbladdress.CollectionArea = 'Queens Park' )
AND ( tbladdress.AddressLine1 = 'Kings Road' )) A
ORDER BY CAST(SUBSTRING(A.[House Number], PATINDEX('%[0-9]%', A.[House Number]),
1 + PATINDEX('%[0-9][^0-9]%', A.[House Number] + ' ') -
PATINDEX('%[0-9]%',A.[House Number])) AS INT)
我正在尝试使用 asp.net 和 c# 在 sql 中按顺序(从 1 到 ...)对列进行排序。在做了一些研究之后,我似乎需要将 SQL 服务器中的列类型更改为 int 但是这是不可能的,因为该列正在存储门牌号,我最终可能会得到门牌号 10a(例如)所以当前设置为varchar。因此,它不能正确地对列进行排序。我已经尝试将相关列转换为 int,但是在运行应用程序时,我在 gridview 中遇到了它的绑定错误,我也尝试了其他的 * 1,但在运行 [=15 时仍然在绑定部分出现错误=]
SQL声明
SELECT DISTINCT tblcontact.ContactID, tblcontact.Forename, tblcontact.Surname,
tbladdress.[House Number], tbladdress.AddressLine1, tbladdress.AddressLine2,
tblcontact.[Business Name] FROM tblcontact INNER JOIN tbladdress ON tblcontact.AddressID = tbladdress.AddressID
LEFT OUTER JOIN tblDonate
ON tblcontact.ContactID = tblDonate.ContactID
WHERE (tbladdress.CollectionArea = @CollectionArea) AND
(tbladdress.AddressLine1 = @drpCollectionStreet)
ORDER BY tbladdress.[House Number] ASC
网格视图标记
<asp:TemplateField HeaderText="House Number">
<EditItemTemplate>
<asp:TextBox ID="txtHouseNum" runat="server" Text='<%# Bind("[House Number]") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblHouseNum" runat="server" Text='<%# Bind("[House Number]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
这是一个如何排序的例子
21
22
27
28
5
6
8
9
这就是我转换为 int 的方式,它在 SQL 中排序良好,但我在 Bind 上遇到错误(上图)
SELECT DISTINCT tblcontact.ContactID, tblcontact.Forename, tblcontact.Surname, cast(tbladdress.[House Number] as int),
tbladdress.AddressLine1, tbladdress.AddressLine2, tblcontact.[Business Name]
FROM tblcontact INNER JOIN tbladdress ON tblcontact.AddressID = tbladdress.AddressID
LEFT OUTER JOIN tblDonate ON tblcontact.ContactID = tblDonate.ContactID
WHERE (tbladdress.CollectionArea = 'Queens Park') AND (tbladdress.AddressLine1 = 'Kings Road')
ORDER BY cast(tbladdress.[House Number] as int)
运行时出错
这是运行时的错误(这个最有意义)
"House Number is neither a DataColumn nor a DataRelation for table DefaultView."}
要按数字对街道号码进行排序,您需要将它们转换为数字。当然,麻烦在于像“10a”这样的字符串不能转换为 INT。答案是从 [House Number] 列中提取数字字符,然后进行转换。以下为 ORDER BY 子句执行此操作:
...
ORDER BY CAST(SUBSTRING(tbladdress.[House Number], PATINDEX('%[0-9]%', tbladdress.[House Number]),
1 + PATINDEX('%[0-9][^0-9]%', tbladdress.[House Number] + ' ') -
PATINDEX('%[0-9]%',tbladdress.[House Number])) AS INT)
请参阅 http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/extracting-numbers-with-sql-server/ 了解复杂公式的工作原理。
从 select column list
中删除 cast
。仅保留 order by
仍然会排序结果。
select A.ContactID,
A.Forename,
A.Surname,
A.[House Number],
A.AddressLine1,
A.AddressLine2,
A.[Business Name] from (
SELECT DISTINCT tblcontact.ContactID,
tblcontact.Forename,
tblcontact.Surname,
tbladdress.[House Number],
tbladdress.AddressLine1,
tbladdress.AddressLine2,
tblcontact.[Business Name]
FROM tblcontact
INNER JOIN tbladdress
ON tblcontact.AddressID = tbladdress.AddressID
LEFT OUTER JOIN tblDonate
ON tblcontact.ContactID = tblDonate.ContactID
WHERE ( tbladdress.CollectionArea = 'Queens Park' )
AND ( tbladdress.AddressLine1 = 'Kings Road' )) A
ORDER BY Cast(tbladdress.[House Number] AS INT)
正确答案是 JohnS 和 NoDisplayName 答案的组合,谢谢你们
select A.ContactID,
A.Forename,
A.Surname,
A.[House Number],
A.AddressLine1,
A.AddressLine2,
A.[Business Name] from (
SELECT DISTINCT tblcontact.ContactID,
tblcontact.Forename,
tblcontact.Surname,
tbladdress.[House Number],
tbladdress.AddressLine1,
tbladdress.AddressLine2,
tblcontact.[Business Name]
FROM tblcontact
INNER JOIN tbladdress
ON tblcontact.AddressID = tbladdress.AddressID
LEFT OUTER JOIN tblDonate
ON tblcontact.ContactID = tblDonate.ContactID
WHERE ( tbladdress.CollectionArea = 'Queens Park' )
AND ( tbladdress.AddressLine1 = 'Kings Road' )) A
ORDER BY CAST(SUBSTRING(A.[House Number], PATINDEX('%[0-9]%', A.[House Number]),
1 + PATINDEX('%[0-9][^0-9]%', A.[House Number] + ' ') -
PATINDEX('%[0-9]%',A.[House Number])) AS INT)