all groups > sql server programming > july 2007 >
You're in the

sql server programming

group:

where first letter = ?


where first letter = ? Middletree
7/7/2007 11:05:09 PM
sql server programming:
I cannot seem to find anything in the Help or BOL about how to do to a
select which would return rows if one of the fields has data which starts
with a particular letter. Of course, I could use LIKE:

select firstname, lastname
from contact
where lastname like (B*)

But I'm thinking that will be pretty slow. There has to be a faster way. Any
help appreciated.

Re: where first letter = ? Middletree
7/7/2007 11:45:10 PM
Thanks. I can't believe I put that asterisk there, when I knew it should be
percent sign. It's late.;)


Re: where first letter = ? Aaron Bertrand [SQL Server MVP]
7/8/2007 12:34:21 AM

[quoted text, click to view]

It will be faster if lastname has an index. The syntax BTW would be:

WHERE lastname LIKE 'B%'

A

Re: where first letter = ? Alex Kuznetsov
7/9/2007 10:52:31 AM
On Jul 7, 11:45 pm, "Middletree" <middlet...@hottttttttmail.com>
[quoted text, click to view]

If it is slow for you, create an index on ( lastname ,firstname)

Alex Kuznetsov, SQL Server MVP
http://sqlserver-tips.blogspot.com/
Re: where first letter = ? Alex Kuznetsov
7/9/2007 10:53:50 AM
[quoted text, click to view]

You approach can be much be slower if there is an index on lastname
and the condition lastname like 'B%' is selective enough.

Alex Kuznetsov, SQL Server MVP
http://sqlserver-tips.blogspot.com/
Re: where first letter = ? Terri
7/9/2007 11:46:58 AM
WHERE LEFT(LastName,1) = 'B'

If I had to guess I would think this would be faster than using 'like' but I
have never tested this.


[quoted text, click to view]

Re: where first letter = ? Jason Lepack
7/9/2007 12:36:33 PM
More often than not you would be wrong. Your solution will not use an
index, so with millions of rows you would be waiting a looooooooooong
time.

[quoted text, click to view]

Re: where first letter = ? Aaron Bertrand [SQL Server MVP]
7/9/2007 4:01:15 PM
[quoted text, click to view]

It would never be faster than like; at best, it would be roughly the same,
but if lastname has an index, your expression renders it useless. You can
try this out pretty easily using the execution plan option in QA / SSMS.

A

AddThis Social Bookmark Button