I was wondering if there is anyway to count the number of days in a month in SQL server 2008... Something similar to this Javascript code I wrote:
function daysInMonth(month, year) {
return new Date(year, month, 0).getDate();
}
var dateForApp = new Date();
var MonthForApp = (dateForApp.getMonth() + 1);
var yearForApp = dateForApp.getFullYear();
var daysofMonth = new Array();
for (var i = 1; i <= daysInMonth(MonthForApp, yearForApp); i++) {
daysofMonth.push(parseInt(i));
} // Resulting in something like this (1,2,3,4,5,6,7,8,9,10,11,12...etc)
I now need to figure out how to do this in SQL... I so far have the foolowing:
declare @Date datetime
select @Date = Getdate()
select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(@Date) as varchar)+'-'+cast(month(@Date) as varchar)+'-01' as datetime))))
which will let me know how many days there are (31) in the month, but I am now not quite sure how to get the actual counting up done... I was trying while loops etc, but have had no success. Has anyone got any ideas or a thread they could point me to? (I found nothing while searching the net)