I want to show all dates between two dates when there is any date data missing then its should show zero in val column .
declare @temp table (
id int identity(1,1) not null,
CDate smalldatetime ,
val int
)
INSERT STATEMENT FOR DATA TO CHECK
insert into @temp select '10/2/2012',1
insert into @temp select '10/3/2012',1
insert into @temp select '10/5/2012',1
insert into @temp select '10/7/2012',2
insert into @temp select '10/9/2012',2
insert into @temp select '10/10/2012',2
insert into @temp select '10/13/2012',2
insert into @temp select '10/15/2012',2
Retrieve records between first day of month and today
select * from @temp where CDate between '10/01/2012' AND '10/15/2012'
As i run this query its show me all data between these two dates but i want to also include missing dates with val=0
You need to make up the dates, so I've use a recursive common table expression here. SQL Fiddle
This will work as long as there are less than 2047 days between from and to dates
using a recursive cte with min and max
I think the best way to do this is to create your own table with dates (you can also use master.dbo.spt_values, but I personally don't like that solution)
you can also use recursive solution with CTE