This may feel very
simple to many developers and yes it is, but I saw this request somewhere in community
website.So thought of creating a very simple
example.
Here I am using AdventureWorks2014 database, we have table [AdventureWorks2014].[HumanResources].[Employee]
and we will use [HireDate] column. I am adding two new columns named HiredMonthName and HiredMonthNumber to make our example clear and simple to
understand. As the name shows HiredMonthName will show the month name from [HireDate]
and HiredMonthNumber will be showing the month number from [HireDate]. I am
using two date functions DatePart and DateName to get the month number and name
respectively. As our requirement we need our resultset to be sorted by month number but it should
display month name. Here I am using both columns in resultset. You can remove
HiredMonthNumber if you wish to. Here is the script and sample output.
SELECT [BusinessEntityID]
,[NationalIDNumber]
,[LoginID]
,[HireDate]
,DateName(Month,[HireDate]) as 'HiredMonthName'
,DatePart(Month,[HireDate]) as 'HiredMonthNumber'
,[OrganizationNode]
,[OrganizationLevel]
,[JobTitle]
,[BirthDate]
,[MaritalStatus]
,[Gender]
,[SalariedFlag]
,[VacationHours]
,[SickLeaveHours]
,[CurrentFlag]
,[rowguid]
,[ModifiedDate]
FROM [AdventureWorks2014].[HumanResources].[Employee]
Order By
HiredMonthNumber ASC