我在 SQL Server 中存储的月份为 1,2,3,4,...12。我想将它们显示为一月、二月等。SQL Server 中是否有类似 MonthName(1) = January 的函数?如果可能的话,我试图避免使用 CASE 语句。
我认为这是当您拥有月份编号时获取月份名称
的最佳方式Select DateName( month , DateAdd( month , @MonthNumber , 0 ) - 1 )
或者
Select DateName( month , DateAdd( month , @MonthNumber , -1 ) )
有点hacky,但应该可以:
SELECT DATENAME(month, DATEADD(month, @mydate-1, CAST('2008-01-01' AS datetime)))
SELECT DATENAME(month, GETDATE()) AS 'Month Name'
SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (@intMonth * 4) - 3, 3)
使用最好的方式
Select DateName( month , DateAdd( month , @MonthNumber , -1 ))
非常简单。
select DATENAME(month, getdate())
产出:一月
从 SQL Server 2012 开始,可以使用 FORMAT 和 DATEFROMPARTS 来解决此问题。 (如果您想要其他文化的月份名称,请更改:
en-US
)
select FORMAT(DATEFROMPARTS(1900, @month_num, 1), 'MMMM', 'en-US')
如果您想要一个包含三个字母的月份:
select FORMAT(DATEFROMPARTS(1900, @month_num, 1), 'MMM', 'en-US')
如果你真的愿意,你可以为此创建一个函数:
CREATE FUNCTION fn_month_num_to_name
(
@month_num tinyint
)
RETURNS varchar(20)
AS
BEGIN
RETURN FORMAT(DATEFROMPARTS(1900, @month_num, 1), 'MMMM', 'en-US')
END
您可以使用内置的
CONVERT
功能
select CONVERT(varchar(3), Date, 100) as Month from MyTable.
这将显示月份的前 3 个字符(JAN、FEB 等..)
除了原来的
SELECT DATENAME(m, str(2) + '/1/2011')
你可以这样做
SELECT DATENAME(m, str([column_name]) + '/1/2011')
通过这种方式,您可以获得表中所有行的名称。其中 [column_name] 表示包含数值 1 到 12 的整数列
2 代表任何整数,通过联系字符串我创建了一个可以提取月份的日期。 '/1/2011' 可以是任何日期
如果你想用变量来做到这一点
DECLARE @integer int;
SET @integer = 6;
SELECT DATENAME(m, str(@integer) + '/1/2011')
以下对我有用:
CAST(GETDATE() AS CHAR(3))
在希伯来语等某些语言环境中,有取决于年份的闰月,因此为了避免在此类语言环境中出现错误,您可以考虑以下解决方案:
SELECT DATENAME(month, STR(YEAR(GETDATE()), 4) + REPLACE(STR(@month, 2), ' ', '0') + '01')
使用此语句将月份数值转换为月份名称。
SELECT CONVERT(CHAR(3), DATENAME(MONTH, GETDATE()))
只需从今天的日期中减去当前月份,然后加回您的月份数字即可。然后使用 datename 函数在 1 行中给出全名。
print datename(month,dateadd(month,-month(getdate()) + 9,getdate()))
这肯定行得通
select datename(M,GETDATE())
SELECT DateName(M, DateAdd(M, @MONTHNUMBER, -1))
要将月份数字转换为月份名称,请尝试以下操作
declare @month smallint = 1
select DateName(mm,DATEADD(mm,@month - 1,0))
您可以使用以下转换功能
CONVERT(VARCHAR(3), DATENAME(MM, GETDATE()), 100)
我认为这足以在你有日期时获得月份名称。
SELECT DATENAME(month ,GETDATE())
SELECT DATENAME(MONTH,dateadd(month, -3,getdate()))
这个对我有用:
@MetricMonthNumber (some number)
SELECT
(DateName( month , DateAdd( month , @MetricMonthNumber - 1 , '1900-01-01' ) )) AS MetricMonthName
FROM TableName
摘自@leoinfo 和@Valentino Vranken 的上述帖子。只是做了一个快速选择,它就起作用了。
Declare @MonthNumber int
SET @MonthNumber=DatePart(Month,GETDATE())
Select DateName( month , DateAdd( month , @MonthNumber , 0 ) - 1 )
说明:
MonthNumber
DatePart
的当前月份,返回月份编号select monthname(curdate());
或
select monthname('2013-12-12');
为我工作
SELECT MONTHNAME(<fieldname>) AS "Month Name" FROM <tablename> WHERE <condition>
你可以这样得到日期。 例如:- 用户表
id name created_at
1 abc 2017-09-16
2 xyz 2017-06-10
你可以这样得到月份名称
select year(created_at), monthname(created_at) from users;
输出
+-----------+-------------------------------+
| year(created_at) | monthname(created_at) |
+-----------+-------------------------------+
| 2017 | september |
| 2017 | june |
您可以创建一个像这样的函数来生成月份并执行 SELECT dbo.fn_GetMonthFromDate(date_column) 作为月份 FROM table_name
/****** Object: UserDefinedFunction [dbo].[fn_GetMonthFromDate] Script Date: 11/16/2018 10:26:33 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fn_GetMonthFromDate]
(@date datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE @monthPart int
SET @monthPart = MONTH(@date)
IF @monthPart = 1
BEGIN
RETURN 'January'
END
ELSE IF @monthPart = 2
BEGIN
RETURN 'February'
END
ELSE IF @monthPart = 3
BEGIN
RETURN 'March'
END
ELSE IF @monthPart = 4
BEGIN
RETURN 'April'
END
ELSE IF @monthPart = 5
BEGIN
RETURN 'May'
END
ELSE IF @monthPart = 6
BEGIN
RETURN 'June'
END
ELSE IF @monthPart = 7
BEGIN
RETURN 'July'
END
ELSE IF @monthPart = 8
BEGIN
RETURN 'August'
END
ELSE IF @monthPart = 9
BEGIN
RETURN 'September'
END
ELSE IF @monthPart = 10
BEGIN
RETURN 'October'
END
ELSE IF @monthPart = 11
BEGIN
RETURN 'November'
END
ELSE IF @monthPart = 12
BEGIN
RETURN 'December'
END
RETURN NULL END
这是我的解决方案,使用其他人的一些信息来解决问题。
datename(month,dateadd(month,datepart(month,Help_HelpMain.Ticket_Closed_Date),-1)) as monthname
SQL Server 中没有系统定义的函数。但是您可以创建自己的用户定义函数 - 标量函数。您可以在数据库的对象资源管理器中找到标量函数:可编程性->函数->标量值函数。下面,我使用一个表变量将它们组合在一起。
--Create the user-defined function
CREATE FUNCTION getmonth (@num int)
RETURNS varchar(9) --since 'September' is the longest string, length 9
AS
BEGIN
DECLARE @intMonth Table (num int PRIMARY KEY IDENTITY(1,1), month varchar(9))
INSERT INTO @intMonth VALUES ('January'), ('February'), ('March'), ('April'), ('May')
, ('June'), ('July'), ('August') ,('September'), ('October')
, ('November'), ('December')
RETURN (SELECT I.month
FROM @intMonth I
WHERE I.num = @num)
END
GO
--Use the function for various months
SELECT dbo.getmonth(4) AS [Month]
SELECT dbo.getmonth(5) AS [Month]
SELECT dbo.getmonth(6) AS [Month]
这就是我用的:
SELECT TRIM(SUBSTRING('January February March April May June July August SeptemberOctober November December ', @MonthNumber * 9 - 8,9))
SQL Server 现在可以使用 STRING_SPLIT 函数获取逗号分隔值的序数
月份的名称也已存储在 SQL Server 中(使用:
SELECT month FROM sys.syslanguages
)
可以通过加入此结果来将月份数字转换为月份名称
select value, ordinal
from sys.syslanguages
cross apply string_split(months,',',1)
where name='Nederlands'
输出:
价值 | 序数 |
---|---|
一月 | 1 |
二月 | 2 |
马特 | 3 |
四月 | 4 |
美 | 5 |
朱尼 | 6 |
朱丽 | 7 |
奥古斯都 | 8 |
九月 | 9 |
十月 | 10 |
十一月 | 11 |
十二月 | 12 |
参见,对于荷兰语以外的其他语言:DBFIDDLE
to_char(to_date(V_MONTH_NUM,'MM'),'MONTH')
其中
V_MONTH_NUM
是月份数字
SELECT to_char(to_date(V_MONTH_NUM,'MM'),'MONTH') from dual;