显示月份名称而不是月份编号 SQL 服务器
Displaying Month Name Instead of Month Number SQL Server
我有一个 select 声明,我正在其中提取日期。假设我在 table 中的日期是 2014-12-25
,我希望这个实际上是 return December 2015
。 SQL 是否有一个函数可以以干净的方式做到这一点?这是我的 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'
我想要 return amount
和 month name and year
,有什么建议吗?
使用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'
或者如果您希望结果在单列中,则使用它。
DATENAME(Month,a.[Date]) +' '+ convert(varchar(4),Year(a.[Date])) As Month_Year
我有一个 select 声明,我正在其中提取日期。假设我在 table 中的日期是 2014-12-25
,我希望这个实际上是 return December 2015
。 SQL 是否有一个函数可以以干净的方式做到这一点?这是我的 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'
我想要 return amount
和 month name and year
,有什么建议吗?
使用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'
或者如果您希望结果在单列中,则使用它。
DATENAME(Month,a.[Date]) +' '+ convert(varchar(4),Year(a.[Date])) As Month_Year