Feckmore
Feckmore

Reputation: 4734

Is there a difference in performance between LTRIM(RTRIM(column_name)) and RTRIM(LTRIM(column_name))

When trimming both ends of a string in SQL Server, is there a performance difference, or any other reason to prefer nesting the LTRIM versus the RTRIM function when building a where clause?

For example:

WHERE RTRIM(LTRIM(SalesPerson)) <> ''

Upvotes: 3

Views: 4706

Answers (1)

t-clausen.dk
t-clausen.dk

Reputation: 44336

Your query can be replaced with:

WHERE SalesPerson <> ''

Even when there are spaces in '' the check will give the same result. Trimming in this case gives no meaning, and will probably slow your query, since you are comparing with a calculation on the column.

Reversing Ltrim and Rtrim should give no performance difference.

Upvotes: 5

Related Questions