Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
deepakugale
Starting Member
33 Posts |
Posted - 2010-05-06 : 07:57:21
|
| Hi Experts , I want to write a query which will take month and year as an input,and will give result as day wise row For eg . If "05" and "2010" as input then output should be like 05/01/2010 1 05/02/2010 2 05/03/2010 3 05/04/2010 4 05/05/2010 5. ..... 05/31/2010 31 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-05-06 : 08:01:36
|
| You mean you want to populate a calander ?Regards,bohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
apodemus
Starting Member
30 Posts |
Posted - 2010-05-06 : 08:13:36
|
| create procedure GetDays (@Month int, @Year int)ASBEGINDECLARE @Days TABLE (day datetime)DECLARE @day intset @day = 1WHILE @day <= 31begin tryinsert into @Days (day)select cast(@month as varchar(2)) + '/'+CAST(@Day as varchar(2))+ '/' + cast(@year as varchar(10))set @day = @day + 1;end trybegin catchselect * from @Daysreturn end catchselect * from @DaysENDexec GetDays 2, 2012apodemus |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-05-06 : 08:18:49
|
| Try this:Create PROCEDURE dbo.usp_GenerateCal@Month Varchar(10), @Year Varchar(4)--@startdate DATETIME ,@enddate DATETIME ,@period int, @value1 int, @value2 intASBEGINDeclare @startdate datetimeDeclare @EndDate datetimeDeclare @DayDiff IntDeclare @CalTable Table (RunDate datetime)Print @Year + '-' +@Month + '-' + '01'Set @startdate = Convert(Datetime, @Year + '-' +@Month + '-' + '01',120)print @startdateSet @DayDiff = Datediff(dd,@startdate,DateAdd(M,1,@startdate) )print @DayDiffSet @enddate = DateAdd(dd,@DayDiff - 1, @startdate );WITH DatesCTE(CurrentDate) AS(SELECT @startdate AS CurrentDateUNION ALLSELECT DATEADD(day,1,CurrentDate)FROM DatesCTEWHERE CurrentDate < @enddate)INSERT INTO @CalTable (RunDate)SELECT CurrentDate FROM DatesCTEOPTION (MAXRECURSION 0)Select * from @CalTableENDGO --Exec usp_GenerateCal '05','2010'Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
|
|
|
|
|