Solved

retrieving all customers whose surname begins with a certain letter

Posted on 2007-03-17
5
542 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

778 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