在 SQL Server 查询中返回月份名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5650830/
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
Returning Month Name in SQL Server Query
提问by Casey DiBroaggio
Using SQL Server 2008
, I have a query that is used to create a view and I'm trying to display a month's nameinstead of an integer.
使用SQL Server 2008
,我有一个用于创建视图的查询,我试图显示月份的名称而不是整数。
In my database, the datetime
is in a column called OrderDateTime
. The lines in the query that return the date is:
在我的数据库中, 位于datetime
名为 的列中OrderDateTime
。查询中返回日期的行是:
DATENAME(yyyy, S0.OrderDateTime) AS OrderYear,
DATEPART(MONTH, S0.OrderDateTime) AS OrderMonth
This returns a column of years and a column of months as integers. I want to return the month names (Jan, Feb, etc
). I've tried:
这将返回一列年份和一列月份作为整数。我想返回月份名称(Jan, Feb, etc
)。我试过了:
CONVERT(varchar(3), DATEPART(MONTH, S0.OrderDateTime) AS OrderMonth
This is obviously is incorrect, as I get
这显然是不正确的,因为我得到
Incorrect syntax near 'AS'
“AS”附近的语法不正确
message. What is the proper syntax for my query?
信息。我的查询的正确语法是什么?
回答by Mikael Eriksson
This will give you the full name of the month.
这将为您提供月份的全名。
select datename(month, S0.OrderDateTime)
If you only want the first three letters you can use this
如果你只想要前三个字母,你可以使用这个
select convert(char(3), S0.OrderDateTime, 0)
回答by nithins
Have you tried DATENAME(MONTH, S0.OrderDateTime)
?
你试过DATENAME(MONTH, S0.OrderDateTime)
吗?
回答by Jim Harris
Change:
改变:
CONVERT(varchar(3), DATEPART(MONTH, S0.OrderDateTime) AS OrderMonth
To:
到:
CONVERT(varchar(3), DATENAME(MONTH, S0.OrderDateTime)) AS OrderMonth
回答by Jeyaprakash
Try this:
尝试这个:
SELECT LEFT(DATENAME(MONTH,Getdate()),3)
回答by Marlon Taborda
In SQL Server 2012 it is possible to use FORMAT(@mydate, 'MMMM') AS MonthName
在 SQL Server 2012 中,可以使用 FORMAT(@mydate, 'MMMM') AS MonthName
回答by Dilip Chauhan
Select SUBSTRING (convert(varchar,S0.OrderDateTime,100),1,3)
from your Table Name
SUBSTRING (convert(varchar,S0.OrderDateTime,100),1,3)
从您的表名中选择
回答by alvin
SELECT MONTHNAME( `col1` ) FROM `table_name`
回答by Bamidelz
This will give you what u are requesting for:
这将为您提供您所要求的:
select convert(varchar(3),datename(month, S0.OrderDateTime))
回答by Mou
Without hitting db we can fetch all months name.
无需点击 db 我们就可以获取所有月份的名称。
WITH CTE_Sample1 AS
(
Select 0 as MonthNumber
UNION ALL
select MonthNumber+1 FROM CTE_Sample1
WHERE MonthNumber+1<12
)
Select DateName( month , DateAdd( month , MonthNumber ,0 ) ) from CTE_Sample1
回答by stubs
basically this ...
基本上这个...
declare @currentdate datetime = getdate()
select left(datename(month,DATEADD(MONTH, -1, GETDATE())),3)
union all
select left(datename(month,(DATEADD(MONTH, -2, GETDATE()))),3)
union all
select left(datename(month,(DATEADD(MONTH, -3, GETDATE()))),3)