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

时间:2015-02-21 02:42:05

标签: c# sql-server format

我有一个选择声明我要拉日期。假设我在表格中的日期为2014-12-25,我希望此实际返回December 2015。是否有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'

我希望这可以返回amountmonth name and year,有什么建议吗?

1 个答案:

答案 0 :(得分:1)

使用 DATENAME 功能

  

返回表示指定datepart的字符串   指定的日期

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'

或者如果您希望将结果放在单列中,请使用此。

DATENAME(Month,a.[Date]) +' '+ convert(varchar(4),Year(a.[Date])) As Month_Year