SQL is a descriptive language, not a procedural language. The methods of the cross apply
logically explain how the results are generated. They do not specify the execution plan. The query optimizer is free to choose any query plan that generates the correct result set.
You need to look at the execution plans for the two queries. My guess is that they are different in a fundamental way. When dealing with type conversions, you should not depend on where
clauses to filter before the conversion. Instead, you should use case
. Your outer query should look like:
(case when [Message] like 'Upload End. Total Row:%'
then CAST(REPLACE([Message], 'Upload End. Total Row:', '') AS INT)
when isnumeric([Message]) = 1
then cast([Message] as int)
end) as row_count;