Reputation: 533
I have a select statement where I am pulling the date. Say my date in the table is 2014-12-25
, I want this to actually return December 2015
. Is there a function that SQL has that can do this in a clean way? Here is my select:
select a.[amount] AS 'Amount', a.[Date] AS 'Month'
FROM [myFirstTable] a
left join [mySecondTable] b on a.[ID] = b.[ID]
left join [myThirdTable] c on c.[code] = b.[code]
where c.[myName] = 'John Doe'
I would like this to return the amount
and month name and year
, any suggestions?
Upvotes: 0
Views: 406
Reputation: 93694
Use DATENAME function
Returns a character string that represents the specified datepart of the specified date
select a.[amount] AS Amount,
DATENAME(Month,a.[Date]) AS [Month],
Year(a.[Date]) as Year
FROM [myFirstTable] a
left join [mySecondTable] b on a.[ID] = b.[ID]
left join [myThirdTable] c on c.[code] = b.[code]
where c.[myName] = 'John Doe'
or if you want the result in single column then use this.
DATENAME(Month,a.[Date]) +' '+ convert(varchar(4),Year(a.[Date])) As Month_Year
Upvotes: 1