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

后端 未结 9 1029
忘了有多久
忘了有多久 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:11

    First day of the month: SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)

    Last day of the month: SELECT DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))

    Substitute a DateTime variable value for GETDATE().

    I got that long ago from this very handy page which has a whole bunch of other date calculations, such as "Monday of the current week" and "first Monday of the month".

提交回复
热议问题