Solved

retrieving all customers whose surname begins with a certain letter

Posted on 2007-03-17
5
546 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 350 total points
ID: 18741374
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
ID: 18741378
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
ID: 18741389
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
ID: 18741491
do you really need dynamic sql...

you can code it statically as

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

 
0
 

Author Comment

by:andieje
ID: 18743157
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

705 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