Thanks for the feedback.
I cleaned up the variables and use smalldatetime instead of nvarchar for the
comparison.
ALTER PROCEDURE [dbo].[SP_REPORT_GET_STATUS_DATES]
@Statement_Year SMALLINT,
@Statement_Month TINYINT
AS
BEGIN
declare @Results table (Group_Code nvarchar(10) not null,
Group_Member_Code nvarchar(10) not null,
Stmt_Date_Created nvarchar(10) null,
Stmt_Date_Updated nvarchar(10) null,
Stmt_Date_Approved nvarchar(10) null,
Supp_Date_Entered nvarchar(200) null,
Composite_Date_Pushed nvarchar(200) null,
Group_Composite_Date_Approved nvarchar(200) null)
------------------------------------------------------------------------
-----------------------
-- Insert the Statement Dates for a particular Group, month, Year in to
the final return table
------------------------------------------------------------------------
-----------------------
Insert into @Results (Group_Code,Group_Member_Code, Stmt_Date_Created,
Stmt_Date_Updated, Stmt_Date_Approved)
Select Group_Code,Group_Member_Code,
isnull(RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(MM, Date_Created)
), 2) +'/'+RIGHT('0' + CONVERT(VARCHAR(2),DATEPART(dd, Date_Created)), 2),''
) ,
isnull(RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(MM, Date_Updated)
), 2) +'/'+RIGHT('0' + CONVERT(VARCHAR(2),DATEPART(dd, Date_Updated)), 2),''
) ,
isnull(RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(MM, Date_Approved
)), 2) +'/'+RIGHT('0' + CONVERT(VARCHAR(2),DATEPART(dd, Date_Approved)), 2),
'')
From vw_Active_Members M
left join vw_statement s
on m.member_identity_id = s.dealership_id and statement_year = @
Statement_Year and statement_month = @Statement_Month and original_Statement
_specification_id = 0
left join vw_statement_specification ss
on s.statement_specification_id = ss.statement_specification_id
and ss.manufacturer_id = m.manufacturer_id and ss.manufacturer_id not in (40)
------------------------------------------------------------------------
----------------------------
-- Insert Supplemental Date
------------------------------------------------------------------------
----------------------------
Select Group_Member_Code, cast(date_created as smalldatetime) as 'Date_
Created'into #tbSuppDate
From vw_Active_Members M
left join vw_statement_specification ss
on ss.manufacturer_id = m.manufacturer_id and ss.manufacturer_id
not in (40)
left join supplemental_values sv
on sv.statement_specification_id = ss.statement_specification_id
and sv.supplemental_month = @Statement_Month
and sv.supplemental_year = @Statement_Year
and sv.dealership_id = m.member_identity_id
Group By group_member_code, cast(date_created as smalldatetime)
order by group_member_code,date_created
SELECT Distinct p1.Group_Member_Code,
( SELECT cast(RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(MM, Date_
Created)), 2) +'/'+RIGHT('0' + CONVERT(VARCHAR(2),DATEPART(dd, Date_Created)
), 2) as nvarchar(10)) + CASE WHEN Date_Created=Null THEN '' ELSE ', ' END
FROM #tbSuppDate p2
WHERE p2.Group_Member_Code = p1.Group_Member_Code
ORDER BY Date_Created
FOR XML PATH('') ) AS Date_Supp into #Temp
FROM #tbSuppDate p1
GROUP BY Group_Member_Code
update @Results
Set Supp_Date_Entered = case when LEN(t.Date_Supp)>0 then LEFT(t.
Date_Supp,LEN(t.Date_Supp)-1) else '' end
from #Temp t inner join @Results r on t.Group_Member_Code=r.Group_
Member_Code
drop table #tbSuppDate
drop table #Temp
------------------------------------------------------------------------
----------------------------
-- Insert Composite Date
------------------------------------------------------------------------
----------------------------
Select
Group_Member_Code, cast(status_date as smalldatetime) as 'Status_Date'
into #tbCompPushed
From vw_Active_Members M
left join composite c
on m.member_id = c.member_group_id and c.composite_data_type_id
= 1 and c.composite_month = @Statement_Month and c.composite_year = @
Statement_Year
left join status_history sh
on c.composite_id = sh.status_identity_id and sh.status_id = 5
Group By group_member_code,cast(status_date as smalldatetime)
order by group_member_code,status_date
SELECT Distinct p1.Group_Member_Code,
( SELECT cast(RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(MM, Status_
Date)), 2) +'/'+RIGHT('0' + CONVERT(VARCHAR(2),DATEPART(dd, Status_Date)), 2
) as nvarchar(10)) + CASE WHEN Status_Date=Null THEN '' ELSE ', ' END
FROM #tbCompPushed p2
WHERE p2.Group_Member_Code = p1.Group_Member_Code
ORDER BY Status_Date
FOR XML PATH('') ) AS Date_Comp into #Temp1
FROM #tbCompPushed p1
GROUP BY Group_Member_Code
update @Results
Set Composite_Date_Pushed = case when LEN(t1.Date_Comp)>0 then LEFT
(t1.Date_Comp,LEN(t1.Date_Comp)-1) else '' end
from #Temp1 t1 inner join @Results r on t1.Group_Member_Code=r.Group
_Member_Code
drop table #tbCompPushed
drop table #Temp1
------------------------------------------------------------------------
----------------------------
-- Insert Composite Approved Date
------------------------------------------------------------------------
----------------------------
Select
Group_Code, cast(status_date as smalldatetime) as 'Date_Approved' into
#tbCompApproved
From vw_Active_Members M
left join composite c
on m.member_id = c.member_group_id and c.composite_data_type_id
= 1 and c.composite_month = @Statement_Month and c.composite_year = @
Statement_Year
left join Group_Composite_Detail gc
on c.Composite_ID=gc.Composite_ID
left join status_history sh
on gc.group_composite_id = sh.status_identity_id and sh.status_
id = 23
Group By group_code,cast(status_date as smalldatetime)
order by group_code,Date_Approved
SELECT Distinct p1.Group_Code,
( SELECT cast(RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(MM, Date_
Approved)), 2) +'/'+RIGHT('0' + CONVERT(VARCHAR(2),DATEPART(dd, Date_
Approved)), 2) as nvarchar(10)) + CASE WHEN Date_Approved=Null THEN '' ELSE
', ' END
FROM #tbCompApproved p2
WHERE p2.Group_Code = p1.Group_Code
ORDER BY Date_Approved
FOR XML PATH('') ) AS Date_Approved into #Temp2
FROM #tbCompApproved p1
GROUP BY Group_Code
update @Results
Set Group_Composite_Date_Approved = case when Len(t2.Date_Approved)>
0 then LEFT(t2.Date_Approved,LEN(t2.Date_Approved)-1) else '' end
from #Temp2 t2 inner join @Results r on t2.Group_Code=r.Group_Code
Select * from @Results order by Group_Code,Group_Member_Code
drop table #tbCompApproved
drop table #Temp2
END