sql server 获取本年的12个月
时间: 2023-07-15 22:15:13 浏览: 177
可以使用以下 SQL 查询获取当前年份的12个月份:
```sql
SELECT MONTH(GETDATE()) AS MonthNumber, DATENAME(MONTH, GETDATE()) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -1, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -1, GETDATE())) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -2, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -2, GETDATE())) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -3, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -3, GETDATE())) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -4, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -4, GETDATE())) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -5, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -5, GETDATE())) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -6, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -6, GETDATE())) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -7, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -7, GETDATE())) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -8, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -8, GETDATE())) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -9, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -9, GETDATE())) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -10, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -10, GETDATE())) AS MonthName
UNION
SELECT MONTH(DATEADD(MONTH, -11, GETDATE())) AS MonthNumber, DATENAME(MONTH, DATEADD(MONTH, -11, GETDATE())) AS MonthName
ORDER BY MonthNumber
```
这个查询会返回当前年份的12个月份,每个月份都包括一个数字和名称。
阅读全文