Note that there are some explanatory texts on larger screens.

plurals
  1. POIs LTRIM(RTRIM(COALESCE(TextField,''))) Bad?
    primarykey
    data
    text
    <p>I have a very high-traffic table with a <code>char(50)</code> field which takes part in several indexes. This <code>char(50)</code> field allows NULLS, and in that case a NULL value is considered to be the same as a non-NULL, zero-length string for my purposes.</p> <p>I also disregard leading &amp; trailing whitespace and, while I scrub the data before I insert it, it may also be inserted by means beyond my control.</p> <p>I have a sproc that is used to copy data from one table to the main table, and it needs to be high-performance. I need to delete duplicate records before inserting the new data and I am using the method discussed <a href="https://stackoverflow.com/questions/714590/most-efficient-t-sql-delete-for-many-rows">in this thread</a> to perform the deletes.</p> <p>My delete statement looks like this (simplified):</p> <pre><code>delete masterTable from masterTable t join incomingDataTable inc on ( LTRIM(RTRIM(COALESCE(inc.TextField,''))) = LTRIM(RTRIM(COALESCE(t.TextField,''))) ) where LTRIM(RTRIM(COALESCE(t.TextField,''))) &lt;&gt; '' </code></pre> <p>I <a href="http://msdn.microsoft.com/en-us/library/aa964133(SQL.90).aspx" rel="nofollow noreferrer">have read</a> that constructs like LTRIM(RTRIM(...)) are bad. Can my delete statement be improved, and if so, how?</p> <p><strong>EDIT:</strong> Just to clarify, <code>TextField</code> does take part in indexes on both tables. <strong>EDIT2:</strong> <code>TextField</code> is defined as <code>char(50)</code> in both tables. It is not of type <code>TEXT</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload