我正在尝试查询日期格式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
我想要的是从YYYYMM日期格式的下一个12个月的当前日期开始。但我目前的查询是:201705,201706,... 201711,201712,201713,201714,等等。所以到现在你可能已经知道我想要的是:201705,201706,... 201711,201712,201801,201802,等等。
我几乎搜索了所有网络,但没有找到答案。所以我来到这里,希望有人可以帮助我。
提前致谢!
答案 0 :(得分:0)
您可以使用SQL Server的DATEADD
来处理下一年的转换并重置为1月,然后将其转换为您的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 |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
答案 1 :(得分:0)
方便的小片段,因为我永远记不起代码......
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;