我希望按SELECT順序返回SELECTed查詢。這是什麼目前正在返回:SELECT查詢不按所需順序返回
這裏是當前查詢:
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)
號碼不是數字值是一個字符串,返回的順序是好的,只要被視爲一個字符串即可。在一個字符串中,以3開頭的任何數字都會出現在以4開頭的任何數字之前,不管它是3000還是43。 – Gonzix
'ORDER BY Convert(BigInt,[Number])ASC' ...可能想要添加一個ISNUMERIC給它,並將它們設置爲某個默認值 –