SQL Server 2005 Get First and Last date for any Month in any Year

后端 未结 9 932
忘了有多久
忘了有多久 2020-12-30 08:29

I have a stored procedure that has to accept a month as int (1-12) and a year as int. Given those two values, I have to determine the date range of that month. So I need a d

相关标签:
9条回答
  • 2020-12-30 09:24

    Current month last Date:

    select dateadd(dd,-day(dateadd(mm,1,getdate())),dateadd(mm,1,getdate()))
    

    Current month 1st Date:

    select dateadd(dd,-day(getdate())+1,getdate())
    
    0 讨论(0)
  • 2020-12-30 09:29
    DECLARE @Month  int = 3, @Year  int = 2016
    DECLARE @StartDate DATETIME
    DECLARE @EndDate DATETIME
    
    -- First date of month
    SET @StartDate = DATEADD(MONTH, @Month-1, DATEADD(YEAR, @Year-1900, 0));
    -- Last date of month
    SET @EndDate = DATEADD(SS, -1, DATEADD(MONTH, @Month, DATEADD(YEAR, @Year-1900, 0)))
    

    This will return first date and last date with time component.

    0 讨论(0)
  • 2020-12-30 09:30
    select [FirstDay Of The Month] as Text ,convert(varchar,dateadd(d,-(day(getdate()-1)),getdate()),106) 'Date'
    union all
    select [LastDay Of The Month], convert(varchar,dateadd(d,-day(getdate()),dateadd(m,1,getdate())),106)
    
    0 讨论(0)
提交回复
热议问题