I need the following to be a uniqueidentifier so that it will join properly in a larger query. Is there a better way to be doing this?
Here is the SQL
-- Report Totals
-- Set date range and this query will look at each specific Crystal Report and how long each one takes to run.
DECLARE @StartDate DATETIME,
@EndDate DATETIME;
SELECT @StartDate = '10/01/2013', @EndDate = '1/1/2014';
SELECT COUNT(*) AS RunCount, AVG(DATEDIFF(s, SJ.queuedtime, SJ.completetime)) AS TotalTime,
AVG(DATEDIFF(s, SJ.queuedtime, SJ.starttime)) AS WaitTime,
AVG(DATEDIFF(s, SJ.starttime, SJ.completetime)) AS RunTime,
RP.label AS Report, RP.reportTitle
FROM SJob SJ
JOIN RReport R ON CAST(SUBSTRING(SJ.props, CHARINDEX('reportID=', SJ.props, 0) + 9, 36) AS UNIQUEIDENTIFIER) = R.reportID
JOIN RPhysicalReport RP ON R.physicalReportID = RP.physicalReportID
WHERE SJ.queuedtime >= @StartDate and SJ.queuedtime < @EndDate and SJ.jobClass = 'CRWPrint'
GROUP BY RP.label, RP.reportTitle
ORDER BY RunTime DESC
when I run this I get
Msg 8169, Level 16, State 2, Line 9 Conversion failed when converting from a character string to uniqueidentifier.