SQL PHP HTML ASP JavaScript articles and free scripts to download

SQL LIKE query Command

LIKE sql command is used to retrieve or match records in different combinations to get desired result with wildcard. We will try LIKE sql query command and how to use it in our MySQL tables. But the best way to use LIKE query command is to apply it in a text or varchar field along with wildcard %.Here is our table with all the records.  

idnameclassmark
1 John Deo Four 75
2 Max Ruin Three 85
3 Arnold Three 55
4 Krish Star Four 60
5 John Mike Four 60
6 Alex John Four 55
 
We will apply the LIKE command here like this to our name field.

SELECT * FROM student WHERE name LIKE '%John%'

id name class mark
1 John Deo Four 75
5 John Mike Four 60
6 Alex John Four 55
The above result shows that we will get all the names where John word is present in the record. It can be any where. Please note the use of  symbol % in the query. This will match any number of character even zero character before or after the word John. So we are getting the names those starts with John also. We may require the names which only starts with John not inside the name. In other words we want records starting with John only. In this case we will remove the use of  % at the left of word John. Here is the SQL with LIKE command.     

Read how Regular expression is used to Pattern matching

SELECT * FROM student WHERE name LIKE 'John%'

id name class mark
1 John Deo Four 75
5 John Mike Four 60
We can see the result above list out all the names starting with name John. To display the records which does not have specific word John as the beginning we have change our LIKE sql command a little by changing the % position to the end of the word. Here we will allow any character even zero character to the left of the desired word not to the right 

SELECT * FROM student WHERE name LIKE '%John'

id name class mark
6 Alex John Four 55

The above result have desired word John at the end only.

Use of underscore ( _) as wildcard in string matching
We can use underscore as wildcard for one character space and use them along with Like statement and apply to fields. For example we want to collect all the account numbers ending with 044 in a five digit account number field. Here is the query for this.

SELECT * FROM account_master WHERE acc_no LIKE '__044'

We have used two underscores in our query to tell that first two digits can be any thing and it should end with 044.

Underscores as wildcard can be used at any location but one can replace one character only. We can use more than one underscore also inside our query.

Using NOT with LIKE

SELECT * FROM student WHERE name NOT LIKE '%John%'

This way we can use LIKE command with many other commands to get desired output.

Case Sensitive query

As we have seen all the above cases are case insensitive. To match lower only or upper only cases we have to use binary command to make binary matching. Here is an example.

select * from student where name LIKE binary '%A'

Try the same query by not using binary inside it.

Limitation of Like query

Like condition has to match the entire data where as by using Regular Expression REGEXP we can match anywhere within the data

Read how LOCATE command is used to search for records having a string in any place within a field.

Read how LIKE command is used in MSSQL database

Download sql dump of this student table

Google+

navid11-05-2009
I wonder if I can use '%' as below for LIKE command:

declare X nvarchar
declare Y nvarcahr
select *
from dbo.~
where(X '%' like Y)-- <-this line is mentioned
manoj06-06-2009
hey all I am using like command for accesing name as alphabetics
I have written for that as
select * from tbl)country where country like " Textbox1.text " but it is not working properly please guide me
Alex08-06-2009
### to manoj
SELECT * FROM TABLE_NAME WHERE country like '%HERE_GOES_MASK%'

In that query, you should replace the TABLE_NAME with the name of the table you query data from. Also, HERE_GOES_MASK should be changed to the string you want to find
Noel04-07-2009
@manoj - This is how your command should be:

"Select * from tblcountry where country like '%" Textbox1.Text "%'"

OR

you could also remove the "%" before the Textbox1.Text or remove it after, it will depend on your usage.

Hope this helps.
kichu16-07-2009
How will i get names of all those people who have the string 'in' in their names?
Penny27-08-2009
I need to find a specific literal in an Oracle table column which is described as a
"long" datatype. How do I do this, since "like" command can't be used on "long" type columns (can't use "long" column in a where clause of a select statement)? I get the error message:"inconsistent datatypes: expected NUMBER got LONG". Please help!
Indranil12-11-2009
I want to selet more than one row at a time ...I think query should be...
SELECT * FROM TABLENAME WHERE FIELDNAME LIKE 'A%'


But it selecting only the first one ...I 'm working in MSAccess...with Core JAVA-Swing

Can u please help me SIR..........
Deepak03-12-2009
To Kichu..
select * from Table_Name where first_name like '%in%'

Nar03-12-2009
Question PHP/SQL: I have an EXEC that take all my contact list from my Database and put them into a variable. Now I want to select all contact starting with "A" as the last name. And show them into my contact list. I want to do it with all letters "A" , "B" , "C" ... Something like Iphone Contact list , all familly name are in alphatical order. So, can I select and show all name starting by a specific letter? how? thx for answerying
Regalla Naresh Reddy21-12-2009
Hi...! The information is useful. I need somehelp.
I want to get the information like

SELECT * FROM employee WHERE name LIKE '%ram'

where '%ram' is the value of other table which is similar to the value in the current table.
Means i need to retrieve the information from the current table based up on the value of other table.

Can you help how to acheive this .....?
smo21-12-2009
select t1.field1,t1.field2,t2.field1.t2.field2 from t1,t2 where t1.keyfield=t2.keyfield and t2.name like '%Ram'
Here t1 and t2 and two tables and linked by a keyfeild ( t1.keyfield=t2.keyfield)
Perwez Akhter07-01-2010
Dear all,
Hi! Can I run two like in single command line. If Yes Tell me how
Shah Viral13-01-2010
Hello EveryOne

i want to find the Partucular Person name,

WHen i write Down in TextBox ..

Thanks
smo13-01-2010
You can run two like commands by using AND or OR combinations.
Designer21-01-2010
how can i use like query to search for two words (statement) not only when word
example : select # from table_name where column like'% hello world%'
a1326-01-2010
"SELECT * FROM video WHERE category LIKE '%'."$_GET['category']".'%' ORDER BY video_id DESC LIMIT ...... I have no idea where is the problem but it seems that this sql can not work with this statement because the webpage is not loading after this statement can someone tell me where is the problem I know that get[category] is funny or animal
smo26-01-2010
Try by printing the SQL to see what the value of category you are getting. You can also print the error message to see what has gone wrong.
Kingsonprisonic30-01-2010
Like not working in vb 6....

please help

code:

Me.Data1.RecordSource = "select * from phone where name LIKE '% " Me.Text1.Text "%'"
Me.Data1.Refresh

all >,<,>=,<=,and,or,between etc works correctly but like not working.....

what can i do?
manohar04-02-2010
I want to check 4th digit of the account no is zero using sql query
Dimitree24-02-2010
I like to search a general one suppose not the Exact one for that what query is use for Eg
SELECT * FROM [reg1] WHERE ([Name] LIKE '%' @Name '%' )
This will display the name what u search in database
I need the query for one are more search wht is that

sf0903-03-2010
what if i would like to find person's name which contain 2 words only?
Chandrasekaran09-03-2010
Hi,

I am having the table with the column First Name.
Where ever the first name contains 'is' I just wanted to remove these word in that record.
Richard16-04-2010
HI all, I have 3 tables (A,B,C)and 2 querys in the first query I join two tables (A
praveen29-04-2010
hi, thanks for the submission of this since this has helped me a lot... i was confused with the usage of % and _. but now its clear for me... thanks again... hope i will be more benefitted in future from here
Mahendran22-07-2010
@ sf09 try combination of builtin functions like
select name from tableName
where len(name) - len(replace(name,, ')) = 1

this will work for you
chinelo26-07-2010
hi, pls how do i write a query that should give me a result for all last_names that start with J, A and M?
Prince29-08-2010
Hi, can you please tell me how to retrieve names that are starting from either A or B from names table?
ABHISHEK04-09-2010
i want a query which will in output give the name of those employees whose last name ends with e and the query should be written without usibg the like statement
linganathan18-10-2010
i want a query , how to find 3rd highest earner in a table
Bhargav01-11-2010
hai...
i am trying to execute below stored procedure. i will take two arguments as a parameters.
ALTER PROCEDURE S_ListVendor
(
@searchFieldValue varchar(128),
@searchInField varchar(120)
)
AS

SELECT
*

FROM
S_Vendor where @searchInField like @searchFieldValue;
its not executed so plz.. help me
Thank you
praveen04-11-2010
I want to print one field that should not start with the digits. How can i write that logic using the NOT LIKE keyword. or is there any otehr comand can we use. pls answer this question.
srikar vandanapu10-11-2010
how to get the zero record table name in a database
sujitha29-12-2010
I am trying to match first four digits(text)of a column values in a table with another table values(text). Pls write an sql query...
Keyur13-09-2011
I want the list of studebts wgose ids are even...lik 2,4,6...pls help
deepak28-09-2011
hello all,
i want the name of the employees whose name starts with j or k or l or m...............what should be the query........plz help......
Alishah19-04-2012
Hello Every0ne:
I Want To See The Third Letter In Name ..... In MySql Whos Could I ?
Zain08-06-2012
have a column named Name.I want to make a query where i just enter initials of any name and i get the full name.Plz solve my problem.

shamim14-07-2012
Very simple and helpful.I have been benefited from here massively.Thanks a lot.
Ruchi10-08-2012
hiiii
what if i would like to find the data of a person having _ as 2nd letter in his name ex.s_sharma............... using wild cards
subhendu16-08-2012
Use locate command
somasundaram31-08-2012
Hi, If I have the second table like Student.Table but the First part of name or Second Part of name in the name field, Now guide how to compare both the table, My database is a huge one near about 1million record.
Kirk31-08-2012
I am trying to find a string at the end of a field using %XYZ as my search criteria. How do I condition the LIKE operator not to consider trailing blanks? I saw some information on the strip BIF but am not familiar with it (novice using SQL).
MadhaviLatha28-11-2012
Hi, I was create table with 3 columns and insert data, by using SELECT QUERY i get all the 3rd column data, from that i want particular column data. what can i do for that. if any body knows plz update.
Thanks in advance.
sandy11-03-2013
Hi, I was created one company based webapplication in that case i want to apply any one company that result wil be updated in my database and one vacancy will be reduced in my jsp page how can i do what query wil be use it please help me

midhun sudhakar04-04-2013
how will i delete records whose name starts with a or j
mikez16-04-2013
how can i get a data from a table where it should be match with its username.

something like this table:

table 1 (username,password,name)



all i want is when i type the username in a textbox and click a command button, the password will be on the other textbox that correspond to the username.



please write the php source code. thanks alot
nitish21-08-2013
please refer a book in which all simple and useful sql queries are given, as u write in ur tutorials, and thanx its very beneficial
NIraj25-09-2013
what is the code of to fetch data from mysql table according to alphabet
Abinash22-11-2013
How to display the names that end with a particular letter in sql?
Veerbhadra Singh24-01-2014
Please tell me how to find a particular one employer salary in mysql table.

for example find second employer salary.
SRK11-04-2014
question:: to display employees whose names are containing 'A' at 2nd occurrence without using wild characters
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer