【问题标题】:Split string to array and order arr[1] ? - MS SQL将字符串拆分为数组并排序 arr[1] ? - 微软 SQL
【发布时间】:2018-08-29 10:16:02
【问题描述】:

我想以正确的方式订购像 1234_1 (id, index) 这样的值。删除 123_ 然后只订购索引。我怎样才能在 Microsoft SQL 中以正确的方式做到这一点......干杯

【问题讨论】:

  • 在 Microsoft SQL 中。我知道。坏标签。 "SELECT * FROM news ORDER BY "123_1" DESC" 到 "SELECT * FROM news ORDER BY 1 DESC" (?)
  • 您的示例值中没有“123_”。具有所需结果的(文本)值表确实会有所帮助。

标签: sql .net sql-server


【解决方案1】:

尝试使用Substringcharindex

按它订购:

SELECT ...
FROM ...
ORDER BY SUBSTRING(YourColumn, CHARINDEX('_', YourColumn) + 1, LEN(YourColumn)) 

【讨论】:

    【解决方案2】:

    大概,您想按索引数字排序(所以_10_9 之后而不是_1)。

    您可以提取值并转换为数字:

    order by try_convert(int, stuff(col, 1, charindex('_', col), ''))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 2012-02-22
      相关资源
      最近更新 更多