查询每个月有多少天

  偶尔会用到,做个笔记:

DECLARE @currDate DATETIME
SET @currDate = '2024-01-01'
DECLARE @currDays INT
SELECT @currDays = 32-DAY(@currDate-DAY(@currDate)+32)
SELECT CONVERT(NVARCHAR(7),@currDate,23) 当前年月,@currDays 当前月对应的天数

  用循环输出:

DECLARE @currDate DATETIME
SET @currDate = '2024-01-01'
DECLARE @currDays INT
SELECT @currDays = 32-DAY(@currDate-DAY(@currDate)+32)
SELECT CONVERT(NVARCHAR(7),@currDate,23) 当前年月,@currDays 当前月对应的天数
WHILE(@currDays>0)
BEGIN
PRINT @currDays
SET @currDays = @currDays - 1
END

  

   以上。

posted @ 2024-06-24 17:06  顾星河  阅读(71)  评论(0编辑  收藏  举报