Get All the dates of a specific month


 DECLARE @dataforDate DATETIME, @month int
 SET @dataforDate=GETDATE()
 SET @month=DATEPART(MONTH,GETDATE())

 DECLARE @t TABLE ( days DATETIME )
 -- SET NOCOUNT ON added to prevent extra result sets from

 -- interfering with SELECT statements.
        SET NOCOUNT ON;

        WITH    CTE_Days
                  AS ( SELECT   DATEADD(month, @month,
                                        DATEADD(month, -MONTH(@dataforDate),
                                                DATEADD(day,
                                                        -DAY(@dataforDate) + 1,
                                                        CAST(FLOOR(CAST(@dataforDate AS FLOAT)) AS DATETIME)))) Days
                       UNION ALL
                       SELECT   DATEADD(day, 1, Days)
                       FROM     CTE_Days
                       WHERE    Days < DATEADD(day, -1,
                                               DATEADD(month, 1,
                                                       DATEADD(month, @month,
                                                              DATEADD(month,
                                                              -MONTH(@dataforDate),
                                                              DATEADD(day,
                                                              -DAY(@dataforDate)
                                                              + 1,
                                                              CAST(FLOOR(CAST(@dataforDate AS FLOAT)) AS DATETIME))))))
                     )
            INSERT  INTO @t
                    SELECT  *
                    FROM    CTE_Days



   SELECT * FROM @t

Comments

Popular posts from this blog

Grouping Sets, With Rollup and With Cube example

Adding Page brake aganist group values

To Get All the Names of tables and Databases their data have changed in last 24 Hours And Backup those databases