未来12个月的日期格式查询

Date format query for next 12 months

我正在尝试查询有关日期格式 YYYYMM 的问题。我当前的查询是这个。

declare @M0 nvarchar(6);set @M0=(SELECT CONVERT(nvarchar(6), GETDATE(), 112))
declare @M1 nvarchar(6);set @M1=@M0+1;
declare @M2 nvarchar(6);set @M2=@M1+1;
declare @M3 nvarchar(6);set @M3=@M2+1;
declare @M4 nvarchar(6);set @M4=@M3+1;
declare @M5 nvarchar(6);set @M5=@M4+1;
declare @M6 nvarchar(6);set @M6=@M5+1;
declare @M7 nvarchar(6);set @M7=@M6+1;
declare @M8 nvarchar(6);set @M8=@M7+1;
declare @M9 nvarchar(6);set @M9=@M8+1;
declare @M10 nvarchar(6);set @M10=@M9+1;
declare @M11 nvarchar(6);set @M11=@M10+1;
declare @M12 nvarchar(6);set @M12=@M11+1;
select @M0,@M1,@M2,@M3,@M4,@M5,@M6,@M7,@M8,@M9,@M10,@M11,@M12

我要获取的是从当前日期开始的接下来 12 个月的 YYYYMM 日期格式。但是我当前的查询是:201705,201706,...201711,201712,201713,201714,等等。所以现在你可能已经知道我想要得到的是:201705,201706,...201711,201712,201801,201802,等等。

我几乎搜索了整个网络,但没有找到答案。所以我来到这里希望有人能帮助我。

提前致谢!

您可以使用 SQL 服务器的 DATEADD,它将处理滚动到下一年并重置回一月,然后将其转换为您的 YYYYMM 格式,如下所示:

DECLARE @M0 NVARCHAR(6)  = CONVERT( NVARCHAR(6), GETDATE(), 112 );
DECLARE @M1 NVARCHAR(6)  = CONVERT( NVARCHAR(6), DATEADD( MONTH, 1, GETDATE() ), 112 );
DECLARE @M2 NVARCHAR(6)  = CONVERT( NVARCHAR(6), DATEADD( MONTH, 2, GETDATE() ), 112 );
DECLARE @M3 NVARCHAR(6)  = CONVERT( NVARCHAR(6), DATEADD( MONTH, 3, GETDATE() ), 112 );
DECLARE @M4 NVARCHAR(6)  = CONVERT( NVARCHAR(6), DATEADD( MONTH, 4, GETDATE() ), 112 );
DECLARE @M5 NVARCHAR(6)  = CONVERT( NVARCHAR(6), DATEADD( MONTH, 5, GETDATE() ), 112 );
DECLARE @M6 NVARCHAR(6)  = CONVERT( NVARCHAR(6), DATEADD( MONTH, 6, GETDATE() ), 112 );
DECLARE @M7 NVARCHAR(6)  = CONVERT( NVARCHAR(6), DATEADD( MONTH, 7, GETDATE() ), 112 );
DECLARE @M8 NVARCHAR(6)  = CONVERT( NVARCHAR(6), DATEADD( MONTH, 8, GETDATE() ), 112 );
DECLARE @M9 NVARCHAR(6)  = CONVERT( NVARCHAR(6), DATEADD( MONTH, 9, GETDATE() ), 112 );
DECLARE @M10 NVARCHAR(6) = CONVERT( NVARCHAR(6), DATEADD( MONTH, 10, GETDATE() ), 112 );
DECLARE @M11 NVARCHAR(6) = CONVERT( NVARCHAR(6), DATEADD( MONTH, 11, GETDATE() ), 112 );
DECLARE @M12 NVARCHAR(6) = CONVERT( NVARCHAR(6), DATEADD( MONTH, 12, GETDATE() ), 112 );
SELECT @M0, @M1, @M2, @M3, @M4, @M5, @M6, @M7, @M8, @M9, @M10, @M11, @M12

输出为:

+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| 201705 | 201706 | 201707 | 201708 | 201709 | 201710 | 201711 | 201712 | 201801 | 201802 | 201803 | 201804 | 201805 |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+

方便的小片段,因为我永远记不住代码...

        WITH lv0 AS (SELECT 0 g UNION ALL SELECT 0)
            ,lv1 AS (SELECT 0 g FROM lv0 a CROSS JOIN lv0 b) -- 4
            ,lv2 AS (SELECT 0 g FROM lv1 a CROSS JOIN lv1 b) -- 16
            ,lv3 AS (SELECT 0 g FROM lv2 a CROSS JOIN lv2 b) -- 256
            ,Tally (n) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM lv3)
        SELECT  CAST( n AS VARCHAR(MAX)) + ' - ' +  CONVERT(VARCHAR(MAX),GETDATE(), n) xZample
        , 'CONVERT(VARCHAR(MAX),@MindIfWeDanceWitYoDates, '+CAST(n AS VARCHAR(25))+')' AS  _________________________tehcode____________________________
        FROM Tally
        WHERE (n< 200) AND (n%100 IN (0,20,21,120,121,126,127,130,131) OR n%100 <= 14)
        ORDER BY n;