MicheleWeaver
Programmer
I have one table with a list of finished dates (one date for each month a date range). I have another table with a line item having a finished date. I am trying to do an outer join (so I get each month) using datepart, but the results are an inner join. I've tried splitting the date out into month and year fields, but run into the same problem. Any ideas?
CREATE TABLE #FinishedMonths(
FinishedMonth int,
FinishedYear int,
FinishedMonthDate datetime
)
SET @CurrentDate =
cast(cast(DatePart(mm,@DateStart) as varchar(2)) +
'/01/'+
cast(DatePart(yyyy,@DateStart) as varchar(4)) as DateTime)
WHILE @CurrentDate < DateAdd(m,1,@DateEnd)
BEGIN
INSERT #FinishedMonths
VALUES (DatePart(mm,@CurrentDate),DatePart(yyyy,@CurrentDate),@CurrentDate)
SET @CurrentDate = DateAdd(m,1,@CurrentDate)
END
SELECT
FinishedMonthDate,
LI.line_item_no,
LI.Finished_Date
FROM #FinishedMonths
LEFT JOIN Line_Item LI
ON DatePart(mm,FinishedMonth) = DatePart(mm,LI.Finished_Date)
AND DatePart(yyyy,FinishedMonth) = DatePart(yyyy,LI.Finished_Date)
CREATE TABLE #FinishedMonths(
FinishedMonth int,
FinishedYear int,
FinishedMonthDate datetime
)
SET @CurrentDate =
cast(cast(DatePart(mm,@DateStart) as varchar(2)) +
'/01/'+
cast(DatePart(yyyy,@DateStart) as varchar(4)) as DateTime)
WHILE @CurrentDate < DateAdd(m,1,@DateEnd)
BEGIN
INSERT #FinishedMonths
VALUES (DatePart(mm,@CurrentDate),DatePart(yyyy,@CurrentDate),@CurrentDate)
SET @CurrentDate = DateAdd(m,1,@CurrentDate)
END
SELECT
FinishedMonthDate,
LI.line_item_no,
LI.Finished_Date
FROM #FinishedMonths
LEFT JOIN Line_Item LI
ON DatePart(mm,FinishedMonth) = DatePart(mm,LI.Finished_Date)
AND DatePart(yyyy,FinishedMonth) = DatePart(yyyy,LI.Finished_Date)