【问题标题】:numbers not sorting in order数字未按顺序排序
【发布时间】:2015-01-01 20:39:00
【问题描述】:

我正在尝试使用 asp.net 和 c# 在 sql 中按顺序(1 到 ...)对列进行排序。在进行了一些研究之后,似乎我需要将 SQL Server 中的列类型更改为 int,但是这是不可能的,因为该列正在存储门牌号,并且我最终可能会得到 10a 的门牌号(例如),因此目前已设置到varchar。因此,它不能正确地对列进行排序。我尝试将相关列转换为 int,但是在运行应用程序时,我在 gridview 中的绑定出现错误,我还尝试了其他作为 * 1 但运行时绑定部分仍然出错

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."} 

【问题讨论】:

  • 当您说“它没有正确排序列”时,您希望它如何排序,它实际上是如何排序的?你能给出一个 3-5 行的“实际”和“预期”行为的小样本吗?
  • 向我们展示您在何处以及如何转换为 int
  • 对上述问题的回答我希望它按数字升序排序
  • 这可能会有所帮助:stackoverflow.com/questions/18625548/…
  • 如果字段是字符串,那么它将作为字符串排序。您需要绑定到一个整数字段以获取您要查找的排序顺序。尝试时遇到什么错误?

标签: c# asp.net sql-server gridview


【解决方案1】:

要对街道编号进行数字排序,您需要将它们转换为数字。当然,问题在于不能将诸如“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 DISTINCT,则 ORDER BY 项目必须出现在选择列表中。然后当我像上面那样将列 [House Number] 转换为 int 时,我在 Runtime here 中得到一个错误 -> ' (error is above)
【解决方案2】:

从选择 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) 

【讨论】:

  • 这不起作用会导致以下错误(因此将强制转换放在列中的原因) - 如果指定了 SELECT DISTINCT,则 ORDER BY 项目必须出现在选择列表中。
  • 这看起来不错,稍微修改一下 Cast(tbladdress.[House Number] AS INT) 应该是 Cast(A.[House Number] AS INT) - 此外,如果门牌号码是 11b,这将不起作用它无法转换为整数我认为我需要将这个答案和 JohnS 的底部答案结合起来
【解决方案3】:

感谢你们,正确答案是 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    相关资源
    最近更新 更多