?
Solved

retrieving all customers whose surname begins with a certain letter

Posted on 2007-03-17
5
Medium Priority
?
548 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 1400 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 300 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 300 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

762 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