将月数转换为 SQL 中的月名函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/185520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Month Number to Month Name Function in SQL
提问by Saif Khan
I have months stored in SQL Server as 1,2,3,4,...12. I would like to display them as January,February etc. Is there a function in SQL Server like MonthName(1) = January? I am trying to avoid a CASE statement, if possible.
我有几个月在 SQL Server 中存储为 1,2,3,4,...12。我想将它们显示为一月、二月等。SQL Server 中是否有像 MonthName(1) = 一月这样的函数?如果可能,我试图避免使用 CASE 语句。
采纳答案by Alexander Kojevnikov
A little hacky but should work:
有点hacky但应该可以工作:
SELECT DATENAME(month, DATEADD(month, @mydate-1, CAST('2008-01-01' AS datetime)))
回答by leoinfo
I think this is the best way to get the month namewhen you have the month number
我认为这是得到的最好方式月份的名字时,你有一个月数
Select DateName( month , DateAdd( month , @MonthNumber , 0 ) - 1 )
Or
或者
Select DateName( month , DateAdd( month , @MonthNumber , -1 ) )
回答by Dharamvir
SELECT DATENAME(month, GETDATE()) AS 'Month Name'
回答by Darryl Martin
SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (@intMonth * 4) - 3, 3)
回答by Asif
Use the Best way
用最好的方式
Select DateName( month , DateAdd( month , @MonthNumber , -1 ))
回答by Saeed ur Rehman
It is very simple.
这很简单。
select DATENAME(month, getdate())
output : January
产量:一月
回答by Saeed ur Rehman
You can use the inbuilt CONVERT
function
您可以使用内置CONVERT
功能
select CONVERT(varchar(3), Date, 100) as Month from MyTable.
This will display first 3 characters of month (JAN,FEB etc..)
这将显示月份的前 3 个字符(JAN、FEB 等。)
回答by Saeed ur Rehman
in addition to original
除了原来的
SELECT DATENAME(m, str(2) + '/1/2011')
SELECT DATENAME(m, str(2) + '/1/2011')
you can do this
你可以这样做
SELECT DATENAME(m, str([column_name]) + '/1/2011')
SELECT DATENAME(m, str([column_name]) + '/1/2011')
this way you get names for all rows in a table. where [column_name] represents a integer column containing numeric value 1 through 12
通过这种方式,您可以获得表中所有行的名称。其中 [column_name] 表示包含数值 1 到 12 的整数列
2 represents any integer, by contact string i created a date where i can extract the month. '/1/2011' can be any date
2 代表任何整数,通过联系字符串我创建了一个日期,我可以在其中提取月份。'/1/2011' 可以是任何日期
if you want to do this with variable
如果你想用变量来做到这一点
DECLARE @integer int;
SET @integer = 6;
SELECT DATENAME(m, str(@integer) + '/1/2011')
回答by unitario
The following works for me:
以下对我有用:
CAST(GETDATE() AS CHAR(3))
回答by Ashish Singh
Use this statement to convert Month numeric value to Month name.
使用此语句将月份数值转换为月份名称。
SELECT CONVERT(CHAR(3), DATENAME(MONTH, GETDATE()))