1

我想在 select 语句中使用 Order by 子句对以下数据中的 nvarchar 字段进行排序。

1/2013
1/2014
2/2013
3/2013
5/2010
25/2013
115/2013
26/2014

我想按结果排序,如下所示:

5/2010
1/2013
2/2013
3/2013
25/2013
115/2013
1/2014
26/2014

我正在使用以下查询,但它不起作用。

SELECT DebitNote
FROM DebitNote
ORDER BY CONVERT(
     INT, 
     SUBSTRING(debitnote, CHARINDEX('/', debitnote) + 1, 4), 
     CONVERT(INT, SUBSTRING(debitNOte, 0, CHARINDEX('/', debitNOte)))
)
4

2 回答 2

0

试试这个——

DECLARE @DebitNote TABLE (DebitNote VARCHAR(50))

INSERT INTO @DebitNote (DebitNote)
VALUES 
('1/2013'), ('1/2014'), ('2/2013'),
('3/2013'), ('5/2010'),
('25/2013'), ('26/2014')

SELECT DebitNote 
FROM @DebitNote
ORDER BY CAST('1/' + DebitNote AS DATETIME)

输出 -

DebitNote
-----------------
5/2010
1/2013
2/2013
3/2013
25/2013
1/2014
26/2014

更新:

DECLARE @DebitNote TABLE (DebitNote VARCHAR(50))
INSERT INTO @DebitNote (DebitNote)
VALUES 
     ('1/2013'), ('1/2014'),
     ('2/2013'), ('3/2013'),
     ('5/2010'), ('25/2013'),
     ('26/2014'), ('116/2013'),
     ('115/2013'), ('315/2014')

SELECT DebitNote
FROM @DebitNote
ORDER BY 
       SUBSTRING(DebitNote, CHARINDEX('/', DebitNote) + 1, 4)
     , CONVERT(INT, SUBSTRING(DebitNote, 0, CHARINDEX('/', DebitNote)))
于 2013-07-29T07:09:57.420 回答
0

根据经验,我编写代码的方式是我在 12 个月内不费吹灰之力就能理解的方式。同样,这意味着我希望其他开发人员也能轻松理解这件事。

所以在这种情况下,我不会在ORDER BY子句中隐藏这个神秘的逻辑。SELECT通过将值作为子句的一部分,我会让事情变得更加透明。

DECLARE @t table (
   DebitNote nvarchar(10)
);

INSERT INTO @t (DebitNote)
  VALUES ('1/2013')
       , ('1/2014')
       , ('2/2013')
       , ('3/2013')
       , ('5/2010')
       , ('25/2013')
       , ('115/2013')
       , ('26/2014');

SELECT DebitNote
FROM   (
        SELECT DebitNote
             , CharIndex('/', DebitNote) As position_of_slash
             , Cast(SubString(DebitNote, 0, CharIndex('/', DebitNote)) As int) As first_part
             , SubString(DebitNote, CharIndex('/', DebitNote) + 1, 10) As last_part -- Note that the last parameter of the SubString() function here
                                                                                    -- equals the length of the field in question
        FROM   @t
       ) As x
ORDER
    BY last_part
     , first_part;

如果愿意,可以在 CTE 中进行最后一个查询

; WITH component_parts AS (
  SELECT DebitNote
       , CharIndex('/', DebitNote) As position_of_slash
       , Cast(SubString(DebitNote, 0, CharIndex('/', DebitNote)) As int) As first_part
       , SubString(DebitNote, CharIndex('/', DebitNote) + 1, 10) As last_part -- Note that the last parameter of the SubString() function here
                                                                              -- equals the length of the field in question
  FROM   @t
)
SELECT DebitNote
FROM   component_parts
ORDER
    BY last_part
     , first_part;
于 2013-07-29T07:35:44.547 回答