Solved

retrieving all customers whose surname begins with a certain letter

Posted on 2007-03-17
5
539 Views
Last Modified: 2012-05-05
Hi

How would i write a select query to retrieve all of the rows in my customer table whose surname begins with a certain letter (e.g. all the customers whose surname starts with 'E')

The query will be built using dynamic sql and the letter will be stored in a variable.

thanks
andrea
0
Comment
Question by:andieje
5 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 350 total points
Comment Utility
declare @Letter varchar(1)
declare @SQL nvarchar(3000)

SET @Letter ='E'

SET @SQL = 'SELECT * FROM MyTable WHERE surname LIKE ''' + @Letter + '%'''
EXEC (@SQL)
0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 75 total points
Comment Utility
Too bad you have to do dynamic SQL.

I'm going to assume the surname is in its own field ...

Select * from MyTable Where SurName like 'E%'

to use a variable so you might avoid the dynamic SQL ...

Select * From Mytable Where Surname like @FirstLetter + '%'

0
 
LVL 29

Expert Comment

by:Nightman
Comment Utility
Just remember that dynamic SQL comes with the overhead of no cached query plan, and if used in a stored procedure the user account executing the SQL needs select permissions on the underlying tables, instead of simply stored proc execute rights. Also is a performance overhead in terms of recompiles (CPU) and opens you up to the risk of SQL injection.
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 75 total points
Comment Utility
do you really need dynamic sql...

you can code it statically as

select *
 from yourtable
where surname like @yourstartname +'%'

 
0
 

Author Comment

by:andieje
Comment Utility
i'm using an or mapper where i can supply the query as a string containing a where clause. the or mapper cleans the data to prevent sql injection attacks

thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now