On several SQL queries I need to check if a field starts with a character.
There are several ways to do it, which one is better in performance/standard?
I usually use
tb.field LIKE 'C%'
but I can also use
LEFT(LTRIM(tb.Field),1) = 'C'
I know well the uses of each case, but not in terms of performance.
I’d go with the first one
LIKE C%, it’ll use an index on the field if there is one rather than having to do a full table scan.If you really need to include the whitespace
LTRIMtrimming in the query, you could create a persisted computed column with the valueLEFT(LTRIM(tb.Field), 1)and put an index on it.