Wildcardsin SQL

Use of Wildcards in SQL

In SQl wildcards are used with LIKE operator which are substitute for any other character or characters . In SQL we can use two wildcards with LIKE operator which are percentage symbol ( %, ) and underscore symbol ( _ ) . Where percentage symbol ( % ) is substitutes for zero or more characters and Underscore symbol ( _ ) are used as substitutes of any single character.

If we want to search the record with name of the person but we not have exact name , we only know the part of name like that the second name is ' Bloch '  then we use LIKE operator with % wildcard before the name:

For example from table given below:

Actual Table

client_name city order_amount
Syed Tauqeer Karachi 50000
Muhammad Zia Ahmed Lahore 20000
Noman Bloch Karachi 30000
Imran Hadir Islamabad 10000
Muneer Bloch Karachi 15000
Muhammad Zia Khan Islamabad 30000

SELECT  *  FROM client_info WHERE client_name like '%Bloch'

This statement show that second part of name or last part of name is Bloch and before the Bloch we did not know

The above statement display the following record.

Selected record with BETWEEN Operator

client_name city order_amount
Noman Bloch Karachi 30000
Muneer Bloch Karachi 15000

we can also use :

SELECT  *  FROM client_info WHERE client_name like 'Syed%'

This statement show that First part of name is Syed and after the Syed we did not know. in simple word we asking to SQl that retune those record whose client-name start form Syed. So its return all those record which start form Syed. In our table only one record will be selected

client_name city order_amount
Syed Tauqeer Karachi 50000

another example is :

SELECT  *  FROM client_info WHERE client_name like '%Zia%'

So we are asking to search those record where client_name have this text Zia. Before or after any thing is possible. So in the result following two records are display:

client_name city order_amount
Muhammad Zia Ahmed Lahore 20000
Muhammad Zia Khan Islamabad 30000

we can also use the underscore ( _ ) wildcards for example:

ELECT  *  FROM client_info WHERE city like '_ahore'

So we are asking to search those record where city name have _ahore. only one character at the place of Underscore is missing. So in the result following one record is display :

client_name city order_amount
Muhammad Zia Ahmed Lahore 20000