显示月份名称而不是月份编号SQL Server

时间:2021-03-12 08:53:37

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:

我有一个选择声明我在拉日期。说我在表中的日期是2014-12-25,我希望这实际上返回到2015年12月。是否有一个SQL可以以干净的方式做到这一点的功能?这是我的选择:

 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?

我希望这可以返回金额和月份名称和年份,任何建议?

1 个解决方案

#1


1  

Use DATENAME function

使用DATENAME功能

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

#1


1  

Use DATENAME function

使用DATENAME功能

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