我将月份存储在SQL Server中为1,2,3,4,…12。我想把它们显示为一月,二月等。在SQL Server中有一个函数像MonthName(1) = 1月?如果可能的话,我尽量避免使用CASE语句。


当前回答

你可以这样得到日期。 -用户表

id name created_at
1  abc  2017-09-16
2  xyz  2017-06-10

您可以像这样获得monthname

select year(created_at), monthname(created_at) from users;

输出

+-----------+-------------------------------+
| year(created_at) | monthname(created_at)  |
+-----------+-------------------------------+
|      2017        | september              |
|      2017        | june                   |

其他回答

SELECT DATENAME(month, GETDATE()) AS 'Month Name'

要将月号转换为月名,请尝试以下步骤

declare @month smallint = 1
select DateName(mm,DATEADD(mm,@month - 1,0))

有点俗气,但应该有用:

SELECT DATENAME(month, DATEADD(month, @mydate-1, CAST('2008-01-01' AS datetime)))

用最好的方式

Select DateName( month , DateAdd( month , @MonthNumber , -1 ))
SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (@intMonth * 4) - 3, 3)