Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL "LIKE" clause help

Status
Not open for further replies.

stevepuri

Programmer
Mar 30, 2001
24
0
0
US
Hey,

I've got this SQL code that works fine for searching a field called "Surname" in a table called "tblCustomer" for
the character "S".

Code:
SELECT * FROM tblCustomer
WHERE Surname LIKE 'S'
ORDER BY Surname;

The problem is, that I get all the surnames that contain the "S" character like "Jone(s)" and "A(s)tury" etc.
I just want the surnames that start with "S"
only like "(S)mith" and "(S)tevenson" etc.

Any ideas?

Thanks,
Steve.

 
SELECT * FROM tblCustomer
WHERE Surname LIKE 'S*'
ORDER BY Surname;
 
Hey,

I tried adding on the * but it
now gives no results whatsoever!

Any other ideas?

Thanks,
Steve.
 
I'll give you the "real" problem code.
I am using this query in VBScript as
part of an ASP page:

Code:
Dim query
Dim query1

'query variable below
'contains "S" or another character
query = Request.Form("txtQuery")

'query1 below contains "S*"
'or another character and *
query1 = query & "*"

Dim sql
sql = ""
sql = sql & "SELECT * FROM tblCustomer "
sql = sql & "WHERE Surname LIKE '%" & query1 & "%'"
sql = sql & "ORDER BY Surname;"

I've also tried % instead of * and it dont work!

Thanks,
Steve.
 
Steve,

Going back to the original post this works.

SELECT tblCustomer.Surname
FROM tblCustomer
WHERE (((tblCustomer.Surname) Like "s*"))
GROUP BY tblCustomer.Surname

I used these names

jones
smith
roberts
msith
tripp
dean
seabury

Only these names appear in the query

seabury
smith

Whether this helps.

David
 
Hi Steve,

You just need to change your statement slightly to give:

Dim sql
sql = ""
sql = sql & "SELECT * FROM tblCustomer "
sql = sql & "WHERE Surname LIKE " query1 & "%'"
sql = sql & "ORDER BY Surname;"

The like statement that you had ( sql = sql & "WHERE Surname LIKE '%" & query1 &"%'" will give a LIKE clause of '%S%' which will look for any name with an S in it, where as you want LIKE 'S%' which will only look for names that begin S

HTH

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top