Reputation: 2217
I have a where clause that compares two columns to the following string. Does this concatenation run for every row? Should this string (and concatenation) be left in twice or should I create a variable to hold the result and use that in the WHERE clause?
CONCAT('%', @myVar, 'dr')
Upvotes: 0
Views: 908
Reputation: 25937
I checked the Execution Plan for your expression in a test table present in my database. The table has two nvarchar(50)
columns namely firstname
and fullname
.
I can clearly see that even for three AND
conditions in where clause that I put deliberately, the SQL Server engine is showing 0% cost for both the compute scalar
steps. It is clearly evident of the fact that irrespective of whether you create a separate variable for the concatenation expression or leave it in-line in your where clause it is not going to make any difference.
Upvotes: 1
Reputation: 4175
Create a variable to hold the result and use that in the WHERE clause
Upvotes: 0