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


当前回答

如果有人想在MySQL中得到同样的东西。请查看以下查询。

 SELECT MONTH(STR_TO_DATE('November', '%M'))

这样我得到了所要求的结果。

其他回答

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

declare @month smallint = 1
select DateName(mm,DATEADD(mm,@month - 1,0))
to_char(to_date(V_MONTH_NUM,'MM'),'MONTH')

其中V_MONTH_NUM是月份号

SELECT to_char(to_date(V_MONTH_NUM,'MM'),'MONTH')  from dual;

用最好的方式

Select DateName( month , DateAdd( month , @MonthNumber , -1 ))
Declare @MonthNumber int
SET @MonthNumber=DatePart(Month,GETDATE())
Select DateName( month , DateAdd( month , @MonthNumber , 0 ) - 1 )

解释:

第一次声明变量月号 获取DatePart的当前月份,返回月份号 第三次查询返回月份名称

SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (@intMonth * 4) - 3, 3)