944,095 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 8219
  • ASP.NET RSS
Oct 7th, 2006
0

Syntax error in "like" in sql statement

Expand Post »
Hi,

I've got a problem with searching for record(s) that match the staffID in my "Staff" table in the database. I have a string named mstrSql, a txtStaffID text box and a sql statement that goes

ASP.NET Syntax (Toggle Plain Text)
  1. mstrSql = "Select * from Staff where staffID like '" & txtStaffID.Text.Trim() & "%" & "'"

I ran the code and it returned an error: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'like'. at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)

I've also tried

ASP.NET Syntax (Toggle Plain Text)
  1. mstrSql = "Select * from Staff where staffID like '%" & txtStaffID.Text.Trim() & "%'"

That didn't work either. Can someone please provide me a correct syntax to run the above? Thanks.
Last edited by tgreer; Oct 7th, 2006 at 10:37 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danae is offline Offline
7 posts
since Jun 2006
Oct 7th, 2006
0

Re: Syntax error in "like" in sql statement

What is the type of your StaffId column?
Reputation Points: 12
Solved Threads: 11
Junior Poster in Training
ManicCW is offline Offline
95 posts
since Nov 2005
Oct 8th, 2006
0

Re: Syntax error in "like" in sql statement

Hi ManicCW,

Thanks, but I have solved the problem eventually. The type of my staffID column is varchar with length of 4. The second way which i have posted turned out to be correct, it couldn't run correctly in the first place was because of some mistake in the subsequent code. And, I have tried to replace the '%' sign with '*', thinking it might work. However, it didn't and the '%' sign is correct.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danae is offline Offline
7 posts
since Jun 2006
Oct 8th, 2006
0

Re: Syntax error in "like" in sql statement

Quote ...
it couldn't run correctly in the first place was because of some mistake in the subsequent code.
What mistake? Is the query string what you thought it was? What happens if you print the query instead of run it and then paste it into SQL to run manually?
Reputation Points: 53
Solved Threads: 6
Junior Poster in Training
Inanna is offline Offline
90 posts
since Sep 2006
Oct 9th, 2006
0

Re: Syntax error in "like" in sql statement

Click to Expand / Collapse  Quote originally posted by Inanna ...
What mistake? Is the query string what you thought it was? What happens if you print the query instead of run it and then paste it into SQL to run manually?
Uh, actually it is hard for me to describe my mistake without showing you the entire thing. I actually need to concatenate the sql with another sql statement, like this one
ASP.NET Syntax (Toggle Plain Text)
  1. mstrSql = mstrSql & " where staffID like '%" & txtStaffID.Text.Trim() & "%' "
where
ASP.NET Syntax (Toggle Plain Text)
  1. mstrSql = "Select * from Staff"
And, because I forgot to place a space before the "where" word, so it'd become "Select * from StaffWhere". Some careless mistake. Anyway, this
ASP.NET Syntax (Toggle Plain Text)
  1. mstrSql = "Select * from Staff where staffID like '%" & txtStaffID.Text.Trim() & "%' "
can run. I mean, there's no mistake in the syntax near the "like". Hope I did answer your question. Of course, if you paste the sql into sql query, it would have to be without the '&' and txtStaffID value.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danae is offline Offline
7 posts
since Jun 2006
Oct 10th, 2006
0

Re: Syntax error in "like" in sql statement

Click to Expand / Collapse  Quote originally posted by danae ...
Uh, actually it is hard for me to describe my mistake without showing you the entire thing. I actually need to concatenate the sql with another sql statement, like this one
ASP.NET Syntax (Toggle Plain Text)
  1. mstrSql = mstrSql & " where staffID like '%" & txtStaffID.Text.Trim() & "%' "
where
ASP.NET Syntax (Toggle Plain Text)
  1. mstrSql = "Select * from Staff"
And, because I forgot to place a space before the "where" word, so it'd become "Select * from StaffWhere". Some careless mistake. Anyway, this
ASP.NET Syntax (Toggle Plain Text)
  1. mstrSql = "Select * from Staff where staffID like '%" & txtStaffID.Text.Trim() & "%' "
can run. I mean, there's no mistake in the syntax near the "like". Hope I did answer your question. Of course, if you paste the sql into sql query, it would have to be without the '&' and txtStaffID value.
This is correct. But as an aside, it is not good practice to use the LIKE clause for something like a staffID, which should be unique or a primary key.

But that depends on the application and its design.
Team Colleague
Reputation Points: 211
Solved Threads: 27
Master Poster
Paladine is offline Offline
793 posts
since Feb 2003

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: uploading Images into Database
Next Thread in ASP.NET Forum Timeline: Weird thing with ACCESS....weird





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC