I see a lot of questions posted about this type of conversion, so I thought I would post a general solution to find the first day of the month, the last day of the month, and the first day of the next month (for range queries) from an integer date in the form of YYYYMM.The YYYYMM conversion is checked by a case statement to verify that the YYYYMM values can be converted to a valid datetime value for that column.  The test script includes invalid YYYYMM values to verify these checks./*Convert YYYYMM Integer Date to DatetimeConvert integer dates in the format YYYYMM todatetime values for first and last day of the month,and the first day of the next month.Returns NULL if YYYY part is not between 1753 and 9999, orMM part is not between 1 and 12, or if YYYYMM is negative or null.Returns NULL for FirstDayOfNextMonth if YYYYMM >= 999912.*/select	YYYYMM,	FirstDayOfMonth =	case	when 	YYYYMM between 175301 and 999912 and		YYYYMM%100 between 1 and 12	then	dateadd(mm,(((YYYYMM/100)-1900)*12)+(YYYYMM%100)-1,0)	end,	LastDayOfMonth =	case	when 	YYYYMM between 175301 and 999912 and		YYYYMM%100 between 1 and 12	then	dateadd(mm,(((YYYYMM/100)-1900)*12)+(YYYYMM%100),-1)	end,	FirstDayOfNextMonth =	case	when 	YYYYMM between 175301 and 999911 and		YYYYMM%100 between 1 and 12	then	dateadd(mm,(((YYYYMM/100)-1900)*12)+(YYYYMM%100),0)	endfrom	( -- Test Data	select YYYYMM = 175201 union all	select YYYYMM = 175301 union all	select YYYYMM = NULL union all	select YYYYMM = -200901 union all	select YYYYMM = 200900 union all	select YYYYMM = 200901 union all	select YYYYMM = 200901 union all	select YYYYMM = 200902 union all	select YYYYMM = 200903 union all	select YYYYMM = 200904 union all	select YYYYMM = 200905 union all	select YYYYMM = 200906 union all	select YYYYMM = 200907 union all	select YYYYMM = 200908 union all	select YYYYMM = 200909 union all	select YYYYMM = 200910 union all	select YYYYMM = 200911 union all	select YYYYMM = 200912 union all	select YYYYMM = 200913 union all	select YYYYMM = 999912 union all	select YYYYMM = 1000001	) a
Test Script Results:YYYYYMM      FirstDayOfMonth          LastDayOfMonth           FirstDayOfNextMonth-----------  -----------------------  -----------------------  -----------------------175201       NULL                     NULL                     NULL175301       1753-01-01 00:00:00.000  1753-01-31 00:00:00.000  1753-02-01 00:00:00.000NULL         NULL                     NULL                     NULL-200901      NULL                     NULL                     NULL200900       NULL                     NULL                     NULL200901       2009-01-01 00:00:00.000  2009-01-31 00:00:00.000  2009-02-01 00:00:00.000200901       2009-01-01 00:00:00.000  2009-01-31 00:00:00.000  2009-02-01 00:00:00.000200902       2009-02-01 00:00:00.000  2009-02-28 00:00:00.000  2009-03-01 00:00:00.000200903       2009-03-01 00:00:00.000  2009-03-31 00:00:00.000  2009-04-01 00:00:00.000200904       2009-04-01 00:00:00.000  2009-04-30 00:00:00.000  2009-05-01 00:00:00.000200905       2009-05-01 00:00:00.000  2009-05-31 00:00:00.000  2009-06-01 00:00:00.000200906       2009-06-01 00:00:00.000  2009-06-30 00:00:00.000  2009-07-01 00:00:00.000200907       2009-07-01 00:00:00.000  2009-07-31 00:00:00.000  2009-08-01 00:00:00.000200908       2009-08-01 00:00:00.000  2009-08-31 00:00:00.000  2009-09-01 00:00:00.000200909       2009-09-01 00:00:00.000  2009-09-30 00:00:00.000  2009-10-01 00:00:00.000200910       2009-10-01 00:00:00.000  2009-10-31 00:00:00.000  2009-11-01 00:00:00.000200911       2009-11-01 00:00:00.000  2009-11-30 00:00:00.000  2009-12-01 00:00:00.000200912       2009-12-01 00:00:00.000  2009-12-31 00:00:00.000  2010-01-01 00:00:00.000200913       NULL                     NULL                     NULL999912       9999-12-01 00:00:00.000  9999-12-31 00:00:00.000  NULL1000001      NULL                     NULL                     NULL(21 row(s) affected)
Of course, if the data is so clean that the checks are unnecessary ( ), they could be removed and just the conversion in the “then” part of the case would be needed:
), they could be removed and just the conversion in the “then” part of the case would be needed:select	YYYYMM,	FirstDayOfMonth     = dateadd(mm,(((YYYYMM/100)-1900)*12)+(YYYYMM%100)-1,0),	LastDayOfMonth      = dateadd(mm,(((YYYYMM/100)-1900)*12)+(YYYYMM%100),-1),	FirstDayOfNextMonth = dateadd(mm,(((YYYYMM/100)-1900)*12)+(YYYYMM%100),0)from	( -- Test Data	select YYYYMM = 175301 union all	select YYYYMM = 200802 union all	select YYYYMM = 200901 union all	select YYYYMM = 200901 union all	select YYYYMM = 200902 union all	select YYYYMM = 200903 union all	select YYYYMM = 200904 union all	select YYYYMM = 200905 union all	select YYYYMM = 200906 union all	select YYYYMM = 200907 union all	select YYYYMM = 200908 union all	select YYYYMM = 200909 union all	select YYYYMM = 200910 union all	select YYYYMM = 200911 union all	select YYYYMM = 200912 union all	select YYYYMM = 999911	) a
CODO ERGO SUM