Get All Dates of Given Month and Year in SQL Server

后端 未结 11 1503
梦如初夏
梦如初夏 2021-02-19 11:22

I want to get all dates by declaring month and year in SQL server.

Can anyone please share easy lines of SQL code to get it.

For example:



        
相关标签:
11条回答
  • 2021-02-19 11:51

    You can get all the dates of a month using the following query.

    declare @month int, @year int
    set @month = 2
    set @year = 2008
    
    SELECT
    CAST(CAST(@year AS VARCHAR) + '-' + CAST(@Month AS VARCHAR) + '-01' AS DATETIME) + Number
    FROM master..spt_values
    WHERE type = 'P'
    AND
    (CAST(CAST(@year AS VARCHAR) + '-' + CAST(@Month AS VARCHAR) + '-01' AS DATETIME) + Number )
    <
    DATEADD(mm,1,CAST(CAST(@year AS VARCHAR) + '-' + CAST(@Month AS VARCHAR) + '-01' AS DATETIME) )
    

    Hope this will help you.

    0 讨论(0)
  • 2021-02-19 11:53

    You can't get all days just by declaring the month, you need to add the year as well because of leap years:

    DECLARE @date DATE = getdate()
    
    ;WITH N(N)AS 
    (SELECT 1 FROM(VALUES(1),(1),(1),(1),(1),(1))M(N)),
    tally(N)AS(SELECT ROW_NUMBER()OVER(ORDER BY N.N)FROM N,N a)
    SELECT top(day(EOMONTH(@date)))
      N day,
      dateadd(d,N-1, @date) date
    FROM tally
    

    Another different solution(by t@clausen):

    DECLARE @month AS INT = 5
    DECLARE @Year AS INT = 2016
    
    ;WITH N(N)AS 
    (SELECT 1 FROM(VALUES(1),(1),(1),(1),(1),(1))M(N)),
    tally(N)AS(SELECT ROW_NUMBER()OVER(ORDER BY N.N)FROM N,N a)
    SELECT N day,datefromparts(@year,@month,N) date FROM tally
    WHERE N <= day(EOMONTH(datefromparts(@year,@month,1)))
    
    0 讨论(0)
  • 2021-02-19 11:55
    CREATE FUNCTION fn_GetMonthlyDates
    (
      @ProcessDate smalldatetime
    )
    RETURNS @LOAN TABLE 
    (
     ProcessDate    smalldatetime
    )
    AS
    BEGIN
        DECLARE 
            @Today DATE= @ProcessDate,
            @StartOfMonth DATE ,
            @EndOfMonth DATE;
    
        DECLARE @DateList TABLE (DateLabel VARCHAR(10) );
        SET @EndOfMonth = EOMONTH(@Today);
        SET @StartOfMonth = DATEFROMPARTS(YEAR(@Today), MONTH(@Today), 1);
    
        WHILE @StartOfMonth <= @EndOfMonth
        BEGIN
           INSERT  INTO @DateList
           VALUES  (@StartOfMonth );
           SET @StartOfMonth = DATEADD(DAY, 1, @StartOfMonth);
        END;
    
        INSERT INTO @LOAN(ProcessDate)
        SELECT  DateLabel
        FROM    @DateList; 
    
    RETURN
    END
    
    0 讨论(0)
  • 2021-02-19 12:03
    DECLARE @Today DATE= GETDATE() ,
    @StartOfMonth DATE ,
    @EndOfMonth DATE;
    
    DECLARE @DateList TABLE ( DateLabel VARCHAR(10) );
    SET @EndOfMonth = EOMONTH(GETDATE());
    SET @StartOfMonth = DATEFROMPARTS(YEAR(@Today), MONTH(@Today), 1);
    
    WHILE @StartOfMonth <= @EndOfMonth
    BEGIN
       INSERT  INTO @DateList
       VALUES  ( @StartOfMonth );
       SET @StartOfMonth = DATEADD(DAY, 1, @StartOfMonth);
    END;
    
    SELECT  DateLabel
    FROM    @DateList; 
    
    0 讨论(0)
  • 2021-02-19 12:05

    If you have a date/time column, then use the month() function:

    select t.*
    from t
    where month(datecol) = 5;
    
    0 讨论(0)
提交回复
热议问题