You can also check this out.
declare @t table (ID int, Date date, Time time, Value1 int, Value2 int) insert into @t values (1,'2014-01-01','00:00',10,20 ) , (2,'2014-01-01','00:00',20,5 ), (3,'2014-02-01','00:00',20,5 ) select * from @t ; with cte as ( select id, day(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,DATE)+1,0))) Totaldayofmonth from @t )
source share