2013-10-27 86 views
-5

查詢我想:日期月份年轉換爲newdate SQL,順序按newdate

SELECT Datename(month, c1.date) + ' - ' 
     + Datename(year, c1.date) AS newdate 
FROM cust_ledger AS c1 
WHERE 
GROUP BY Datename(month, c1.date) + ' - ' 
     + Datename(year, c1.date) 
ORDER BY newdate 

將返回:

December-2013 
Janyuary-2013 
November-2013 
Octomber-2013

我想我的結果進行排序:

Janyuary-2013 
Octomber-2013 
November-2013 
December-2013
+3

-1要求解決方案「儘快」,好像你的「問題」是世界上最重要的事情! – geomagas

+0

對於StackOverflow的未來問題,請使用正確的專業英語(避免非wana和文本說話等非單詞),發佈實際代碼'WHERE GROUP BY'肯定無效,絕對不要求任何緊急事件。 –

+1

@MartinSmith清理完畢。 – Kermit

回答

-1

嘗試order by c1.日期::

select 
CONCAT(DATENAME(`month`,c1.`date`),' - ',DATENAME(`year`, c1.`date`)) as newdate 
from cust_ledger as c1 
group by YEAR(ci.`date`),MONTH(c1.`date`) 
order by c1.`date` 
+0

Msg 4104,Level 16,State 1,Line 4 無法綁定多部分標識符「ci.date」。來自cust_ledger的新日期 作爲c1 羣由YEAR(ci.date),MONTH(ci.date),MONTH(ci.date),MONTH(date,c1.date)作爲新日期 – user2925066

+0

選擇 +' - '+ DATENAME c1.date) order by c1.date – user2925066

+0

已使用concat,請檢查 –

1

包括'字段'您想按照您的group by排序並按它們排序。

SELECT Datename(month, c1.date) + ' - ' 
     + Datename(year, c1.date) AS newdate 
FROM cust_ledger AS c1 
GROUP BY Datename(month, c1.date) + ' - ' 
     + Datename(year, c1.date),Datepart(year, c1.date),Datepart(Month, c1.date) 
ORDER BY Datepart(year, c1.date),Datepart(Month, c1.date) 
+1

謝謝veru很多bummi ...... – user2925066

0
SELECT TOP X * 
FROM myTable 
ORDER BY 
    CASE 
     WHEN LEFT(Col , LEN(Col) - 5)= 'December' THEN 12 
     WHEN LEFT(Col , LEN(Col) - 5)= 'November' THEN 11 
     WHEN LEFT(Col , LEN(Col) - 5)= 'October' THEN 10 
     WHEN LEFT(Col , LEN(Col) - 5)= 'September' THEN 9 
     WHEN LEFT(Col , LEN(Col) - 5)= 'August' THEN 8 
     WHEN LEFT(Col , LEN(Col) - 5)= 'July' THEN 7 
     WHEN LEFT(Col , LEN(Col) - 5)= 'June' THEN 6 
     WHEN LEFT(Col , LEN(Col) - 5)= 'May' THEN 5 
     WHEN LEFT(Col , LEN(Col) - 5)= 'April' THEN 4 
     WHEN LEFT(Col , LEN(Col) - 5)= 'March' THEN 3 
     WHEN LEFT(Col , LEN(Col) - 5)= 'February' THEN 2 
     WHEN LEFT(Col , LEN(Col) - 5)= 'January' THEN 1 

    END ASC 

你斯佩林斯在某些情況下,像'Janyuary''Octomber'有點滑稽只是記住這一點,而這代碼應該爲你工作:)

在這裏看到的結果sqlfiddle

+0

檢查http://sqlfiddle.com/#!3/fea94/1 –

相關問題