I have the following sql query which has a cross apply with a sub query.
SELECT
pm.[FileName]
,REPLACE([Message], 'Upload End. Total Row:', '') cnt_char
,CAST(REPLACE([Message], 'Upload End. Total Row:', '') AS INT) AS row_count
FROM
dbo.ProductMaster pm
CROSS APPLY
(SELECT TOP 30 *
FROM dbo.ActivityLog lo
WHERE [Message] like 'Upload End%'
AND lo.[FileName] = pm.[FileName]
ORDER BY ActivityDate DESC) AS s
It runs fine but If I remove the second expression (,REPLACE([Message], 'Upload End. Total Row:', '') cnt_char
) in the select the I get the following error
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'Upload Start' to data type int.
The where clause in the sub query should be specifically excluding all the rows where Message is "Upload Start" but why am I getting this error. Am I not using the CROSS APPLY
properly?