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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get fixed number of rows

Author  Topic 

anujpratap84
Starting Member

45 Posts

Posted - 2012-01-09 : 02:10:27
Hi All,

I want to get fixed number of rows based on passed date range variable.
ex. if i pass jan 2012 to sep 2012 then SP will returns 9 rows with first date column.

If data not exists in databse for any month then that row is not showing in result set, i want each month in result set.

Thanks in advance.

Anuj Pratap Singh

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-09 : 02:31:34
you need to use calendar date function for that.
see an example below

SELECT [Date] FROM dbo.CalendarTable('2012-01-01','2012-09-01',0,1)


http://visakhm.blogspot.com/2010/02/generating-calendar-table.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

anujpratap84
Starting Member

45 Posts

Posted - 2012-01-10 : 02:35:51
Hi visakh16,

Thanks for reply.
Query throwing an error.
Invalid object name 'dbo.CalendarTable'.

I am using sql server 2005.


quote:
Originally posted by visakh16

you need to use calendar date function for that.
see an example below

SELECT [Date] FROM dbo.CalendarTable('2012-01-01','2012-09-01',0,1)


http://visakhm.blogspot.com/2010/02/generating-calendar-table.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Anuj Pratap Singh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-10 : 03:54:39
you need to create the function first by copy pasting the code in the posted link

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -