Hi
Proceed the sample script given below
1. Preparing sample table & dataIF OBJECT_ID('Tb_Sample1','U') IS NULL
CREATE TABLE Tb_Sample1
(
ID INT IDENTITY(1,1),
DateColumn DATETIME
)
GO
TRUNCATE TABLE Tb_Sample1
GO
INSERT Tb_Sample1(DateColumn) VALUES('2010-12-01')
INSERT Tb_Sample1(DateColumn) VALUES('2010-12-02')
INSERT Tb_Sample1(DateColumn) VALUES('2010-12-05')
INSERT Tb_Sample1(DateColumn) VALUES('2010-12-07')
INSERT Tb_Sample1(DateColumn) VALUES('2010-12-08')
INSERT Tb_Sample1(DateColumn) VALUES('2010-12-09')
INSERT Tb_Sample1(DateColumn) VALUES('2010-12-11')Now, we have we have entry for 1,2,5,7,8,9,11 date of December month. In your case, It should result actual data along with missing date entry as NULL.
2. Execute the script given below...;WITH CTEs
AS
(
SELECT 1 AS 'Numbers'
UNION ALL
SELECT Numbers + 1 FROM CTEs WHERE Numbers<=30
)
SELECT A.DateColumn FROM CTEs C LEFT JOIN Tb_Sample1 a(NOLOCK)
ON (DAY(A.DateColumn) = C.Numbers)
The result would be as given below...
DateColumn
---------------------------
2010-12-01 00:00:00.000
2010-12-02 00:00:00.000
NULL
NULL
2010-12-05 00:00:00.000
NULL
2010-12-07 00:00:00.000
2010-12-08 00:00:00.000
2010-12-09 00:00:00.000
NULL
2010-12-11 00:00:00.000
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
Note: we have used 31 days, But you have to include condition based on Months (28,30,31,...Etc)
Cheers
Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions
Suneel161, if this helps please login to Mark As Answer. | Alert Moderator