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 datetime variable to represent the first day of that month, and another datetime variable to represent the last day of that month. Is there a fairly easy way to get this info?
+1
A:
DECLARE @Month int
DECLARE @Year int
set @Month = 2
set @Year = 2004
select DATEADD(month,@Month-1,DATEADD(year,@Year-1900,0)) /*First*/
select DATEADD(day,-1,DATEADD(month,@Month,DATEADD(year,@Year-1900,0))) /*Last*/
But what do you need as time component for last day of the month? If your datetimes have time components other than midnight you may well be better off just doing something like
WHERE COL >= DATEADD(month,@Month-1,DATEADD(year,@Year-1900,0))
AND COL < DATEADD(month,@Month,DATEADD(year,@Year-1900,0))
In this way your code will continue to work if you eventually migrate to SQL Server 2008 and the greater precision datetime datatypes.
Martin Smith
2010-10-12 15:42:09
Thank you. This worked. I preferred this answer as it appears to be the cleanest.
Ristogod
2010-10-12 15:58:12
+1
A:
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".
DOK
2010-10-12 15:42:21
This worked. Although I chose a different answer as I preferred its syntax. The page you linked is a great source of information. Thanks.
Ristogod
2010-10-12 15:57:42
+1
A:
DECLARE @Month int;
DECLARE @Year int;
DECLARE @FirstDayOfMonth DateTime;
DECLARE @LastDayOfMonth DateTime;
SET @Month = 3
SET @Year = 2010
SET @FirstDayOfMonth = CONVERT(datetime, CAST(@Month as varchar) + '/01/' + CAST(@Year as varchar));
SET @LastDayOfMonth = DATEADD(month, 1, CONVERT(datetime, CAST(@Month as varchar)+ '/01/' + CAST(@Year as varchar))) - 1;
John Hartsock
2010-10-12 15:42:38
This worked. Although I chose a different answer as I preferred its syntax and non-use of converting. Thanks.
Ristogod
2010-10-12 15:56:09
+1
A:
DECLARE @Month INTEGER
DECLARE @Year INTEGER
SET @Month = 10
SET @Year = 2010
DECLARE @FirstDayOfMonth DATETIME
DECLARE @LastDayOfMonth DATETIME
SET @FirstDayOfMonth = Str(@Year) + RIGHT('0' + Str(@Month), 2) + '01'
SET @LastDayOfMonth = DATEADD(dd, -1, DATEADD(mm, 1, @FirstDayOfMOnth))
SELECT @FirstDayOfMonth, @LastDayOfMonth
AdaTheDev
2010-10-12 15:43:57
This worked. Although I chose a different answer as I preferred its syntax and non-use of Right. Thanks.
Ristogod
2010-10-12 15:55:49
+1
A:
Try this:
Declare @month int, @year int;
Declare @first DateTime, @last DateTime;
Set @month=10;
Set @year=2010;
Set @first=CAST(CAST(@year AS varchar) + '-' + CAST(@month AS varchar) + '-' + '1' AS DATETIME);
Set @last=DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@first)+1,0));
SELECT @first,@last;
Tim Schmelter
2010-10-12 15:46:41
This worked. Although I chose a different answer as I preferred its syntax and non-use of casting. Thanks.
Ristogod
2010-10-12 15:55:14