我正在嘗試在sql server中按月份YTD。我在2月份沒有任何記錄。如何在sql server 2008中按月份獲取月份名稱?
select WO,CreatedDate,Item ,(DATENAME(Month,CreatedDate)) as [Month],
DATEPART(yyyy ,CreatedDate) as [Year]from #temptbale
Group by WO,CreatedDate,Item
Order by Month
輸出:
WO Date Item Month
WO-1009 2014-04-29 Bicycle - Blue April
WO-1010 2014-04-29 Bicycle - Blue April
WO-1012 2014-01-20 Bicycle - Blue January
WO-1013 2014-01-24 Bicycle - Blue January
WO-1021 2014-03-12 Bicycle - Red March
WO-1022 2014-03-12 Bicycle - Red March
慾望結果:
WO Date Item Month
WO-1009 2014-04-29 Bicycle - Blue April
WO-1010 2014-04-29 Bicycle - Blue April
WO-1012 2014-01-20 Bicycle - Blue January
WO-1013 2014-01-24 Bicycle - Blue January
WO-1021 2014-03-12 Bicycle - Red March
WO-1022 2014-03-12 Bicycle - Red March
NULL NULL NULL February
試由Concat組成(月(CreatedDate),年(CreatedDate))或類似概念的東西。 –