Get All Dates of Given Month and Year in SQL Server

后端 未结 11 1549
梦如初夏
梦如初夏 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条回答
  • WHERE Dates LIKE '2018-12%'
    

    In a datetime or timestamp it goes Year-Month, so this would pull everything that matches 2018 in December. You can mod this to use your variables as well.

    @month = 12;
    @year = 2018;
    @searchQuery = @year + @month + '%';
    
    WHERE Dates LIKE @searchQuery
    
    0 讨论(0)
  • 2021-02-19 11:45

    Same approach as t-clausen, but a more compact:

    Declare @year int = 2017, @month int = 11;
    WITH numbers
    as
    (
        Select 1 as value
        UNion ALL
        Select value + 1 from numbers
        where value + 1 <= Day(EOMONTH(datefromparts(@year,@month,1)))
    )
    SELECT datefromparts(@year,@month,numbers.value) Datum FROM numbers
    
    0 讨论(0)
  • 2021-02-19 11:47
    DECLARE @MonthNo TINYINT = 03 -- set your month
        ,@WOYEAR SMALLINT = 2018; -- set your year
    
    IF OBJECT_ID('TEMPDB..#TMP') IS NOT NULL
        DROP TABLE #TMP
    
    DECLARE @START_DATE DATETIME
        ,@END_DATE DATETIME
        ,@CUR_DATE DATETIME
    
    SET @START_DATE = DATEADD(month, @MonthNo - 1, DATEADD(year, @WOYEAR - 1900, 0))
    SET @END_DATE = DATEADD(day, - 1, DATEADD(month, @MonthNo, DATEADD(year, @WOYEAR - 1900, 0)))
    SET @CUR_DATE = @START_DATE
    
    CREATE TABLE #TMP (
        WEEKDAY VARCHAR(10)
        ,DATE INT
        ,MONTH VARCHAR(10)
        ,YEAR INT
        ,dates VARCHAR(25)
        )
    
    WHILE @CUR_DATE <= @END_DATE
    BEGIN
        INSERT INTO #TMP
        SELECT DATENAME(DW, @CUR_DATE)
            ,DATEPART(DAY, @CUR_DATE)
            ,DATENAME(MONTH, @CUR_DATE)
            ,DATEPART(YEAR, @CUR_DATE)
            ,REPLACE(CONVERT(VARCHAR(9), @CUR_DATE, 6), ' ', '-')
    
        SET @CUR_DATE = DATEADD(DD, 1, @CUR_DATE)
    END
    
    SELECT *
    FROM #TMP
    
    0 讨论(0)
  • 2021-02-19 11:47

    Little modification. Query given by @t-clausen.dk will give you correct result only if you ran it on first of the month. With little change this works awesome.

    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,
     DATEFROMPARTS(year(@date),month(@date), n) date
    FROM tally
    

    BTW nice trick t-clausen.dk. I couldn't think of more easy way

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

    Here is a query:

    DECLARE @ReportDate VARCHAR(20)='2019-02-10'
    
    DECLARE @LastDay INT =DAY(EOMONTH(@ReportDate))
    
    DECLARE @DayStart INT=01
    
    CREATE TABLE #TEMPMonth ([MDay] VARCHAR(20))
    
    WHILE @LastDay>=@DayStart
    
    BEGIN
        
    INSERT INTO #TEMPMonth 
        
    SELECT CAST(CAST(YEAR(@ReportDate)AS VARCHAR)+'-'+CAST(MONTH(@ReportDate)AS VARCHAR)+'-'+CAST(@DayStart AS VARCHAR) AS DATE)
        SET @DayStart+=1
    
    END
    
    SELECT * FROM #TEMPMonth
    
    DROP TABLE #TEMPMonth
    
    0 讨论(0)
  • 2021-02-19 11:50

    Another CTE based version

    DECLARE @Month INT = 2, @Year INT = 2019
    
    ;WITH MonthDays_CTE(DayNum)
    AS
    (
    SELECT DATEFROMPARTS(@Year, @Month, 1) AS DayNum
    UNION ALL
    SELECT DATEADD(DAY, 1, DayNum)
      FROM MonthDays_CTE
      WHERE DayNum < EOMONTH(DATEFROMPARTS(@Year, @Month, 1))
    )
    SELECT *
      FROM MonthDays_CTE
    
    0 讨论(0)
提交回复
热议问题