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

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

    You can use DATEFROMPARTS to declare the first day of the month and EOMONTH to derive the last date of the month from the first date.

    DECLARE @Month INT = 1
    DECLARE @Year INT = 2020
    
    DECLARE @FromDate DATE = DATEFROMPARTS(@Year, @Month, 1)
    DECLARE @ToDate DATE = EOMONTH(@FromDate)
    
    SELECT @FromDate MonthFirstDate, @ToDate MonthLastDate
    

提交回复
热议问题