我想让 SELECTed 查询按数字顺序返回。这是当前正在返回的内容:
这是当前的查询:
SELECT
[Number] + ' - ' + SUBSTRING([Title], 1, 40) AS [StoryName]
,[PK_Story]
FROM Story STY
INNER JOIN Task TSK ON TSK.StoryId = STY.PK_Story
INNER JOIN DailyTaskHours DTH ON DTH.TaskId = TSK.PK_Task
WHERE ProductId = @productIdParam
AND DTH.ActivityDate BETWEEN @startDateParam
AND @endDateParam
GROUP BY [Number]
,[Number] + ' - ' + SUBSTRING([Title], 1, 40)
,[PK_Story]
HAVING SUM(DTH.[Hours]) > 0
ORDER BY [Number] ASC
我正在尝试订购[Number]
哪个是 CMIS-##。如您所见,我ORDER BY
试图这样做,但结果顺序不正确。较低的值“CMIS-43”不在顶部。我该如何解决?
编辑:
这最终是我处理可变长度的解决方案[Number]
:
SELECT [Number] + ' - ' + SUBSTRING([Title], 1, 40) AS [StoryName]
,[PK_Story]
FROM Story STY
INNER JOIN Task TSK ON TSK.StoryId = STY.PK_Story
INNER JOIN DailyTaskHours DTH ON DTH.TaskId = TSK.PK_Task
WHERE ProductId = @productParam
AND DTH.ActivityDate BETWEEN @startDateParam
AND @endDateParam
GROUP BY [Number]
,[Number] + ' - ' + SUBSTRING([Title], 1, 40)
,[PK_Story]
HAVING SUM(DTH.[Hours]) > 0
ORDER BY CAST(SUBSTRING([Number], CHARINDEX('-',[Number]) + 1, LEN([Number])) AS INT)