我需要一个SQL
查询来列出从当月开始的一年中的月份。
我已尝试过此查询,但我需要当前月份作为列表中的第一个。
DECLARE @year int
SET @year = 2016
SELECT number AS a,
DATENAME(MONTH, cast(@year*100+number AS varchar) + '01') AS b
FROM master.dbo.spt_values
WHERE TYPE = 'P'
AND number BETWEEN 1 AND 12
谢谢
答案 0 :(得分:0)
看看这个查询。 它仅用于演示,
[NSNumber numberWithBool:YES]
答案 1 :(得分:0)
这可以使用递归cte:
轻松解决;WITH CTE AS
(
SELECT GETDATE() As TheDate,
DATENAME(MONTH, GETDATE()) As TheMonth
UNION ALL
SELECT DATEADD(MONTH, 1, TheDate),
DATENAME(MONTH, DATEADD(MONTH, 1, TheDate))
FROM CTE
WHERE YEAR(DATEADD(MONTH, 1, TheDate)) = YEAR(GETDATE())
)
SELECT TheMonth
FROM CTE