SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Pattern Matching in a query by using LIKE command

We can use LIKE command inside a query with SELECT & WHERE combination to get records with some string matching. This is of great use while we are developing a search query or any other type of application where we expect to get a matching pattern to the query.

Download the student table with structure and data in csv format for your study and testing. You can use the same display script used there.

Inside a Query LIKE command is used in many combinations. Here we will see how the command is used for queries designed for MSSQL table. There is no great difference here but you can read how the LIKE command is used for MySQL in our sql section.

Here is the query we designed to collect all the records from student table starting with letter J

rs1.open " select * from student WHERE name LIKE 'J%' " , conn


Now let us find out the records ending with a single char e

rs1.open " select * from student WHERE name LIKE '%e' " , conn


Now let us find out the records having two chars inside their name any where.

rs1.open " select * from student WHERE name LIKE '%ff%' " , conn


This way we can use wildcard to get the required pattern combinations in our query. Now let us try to match underscore ( _ ) as a replacement for a single position.

rs1.open " select * from student where mark like '5_' " , conn


The above query will return all the records with mark is between 50 and 59 ( both ends inclusive ) . Here we have specified that the first digit should be 5 and second digit can be any thing. We can sue underscore for more than one place also.
Further readings
SELECT query in collecting records from MSSQL table
TOP with SELECT query to collect number of records
SELECT query with AND OR NOT for MSSQL table
SELECT query with LIKE using wildcards % and _
Count: Counting total records in MSSQL table
Group By: SQL command in MSSQL table
Different Date & Time formats by using Convert in SELECT query
 
Scripts
PHP
JavaScript
All ASP Tutorials
SELECT Query
SELECT
AND OR NOT
count
Date formats
Group By
LIKE & _
TOP
ASP Sections
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
String Functions
Popular Tutorials
Managing two drop downs
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.