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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching a field from the first character like "begins with" 1

Status
Not open for further replies.

rtgordon

Programmer
Jan 17, 2001
104
US
as opposed to using like. I am currently using LIKE in the where clause, but I was asked to accept the user input and search where "part number begins with the user input" as opposed to searching for the string anywhere in the field.

For example, If the user enters "08" I should only return parts beginning with 08.

I am using ASP with SQL Server 7. I suppose that I could do something like take the length of the input string (x), make the query return results where the substr(part_num,1,x) = user_input. Would this work, and is there an easier way that I am not aware of? like part_num BEGINS WITH user_input??

Thanks,
gordon

 
What's wrong with "LIKE"? It was designed for exactly what you're trying to do:

WHERE part_number LIKE 'O8%';

will only bring back information where the part number begins with O8.
 
If you MUST use an alternative approach, I guess you could combine the LENGTH and SUBSTR functions:

WHERE substr(part_number, 1, LENGTH(user_input)) = user_input;

but this seems a little silly.
 
QUOTE:
"WHERE part_number LIKE 'O8%';"


doh! sometimes I wonder about me :-s Thanks for the reply. I remembered that as soon as I looked at your post.

later
gordon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top