WHERE Clause to find all records in a specific month

后端 未结 10 2103
傲寒
傲寒 2020-12-04 10:07

I want to be able to give a stored procedure a Month and Year and have it return everything that happens in that month, how do I do this as I can\'t compare between as some

10条回答
  •  抹茶落季
    2020-12-04 10:22

    I find it easier to pass a value as a temporal data type (e.g. DATETIME) then use temporal functionality, specifically DATEADD and DATEPART, to find the start and end dates for the period, in this case the month e.g. this finds the start date and end date pair for the current month, just substitute CURRENT_TIMESTAMP for you parameter of of type DATETIME (note the 1990-01-01 value is entirely arbitrary):

    SELECT DATEADD(M, 
              DATEDIFF(M, '1990-01-01T00:00:00.000', CURRENT_TIMESTAMP), 
              '1990-01-01T00:00:00.000'), 
           DATEADD(M, 
              DATEDIFF(M, '1990-01-01T00:00:00.000', CURRENT_TIMESTAMP), 
              '1990-01-31T23:59:59.997')
    

提交回复
热议问题