Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having trouble writing an sql query with 2 WHERE clauses. This is my current query,
("SELECT * FROM Sales WHERE CD/DVD = '" + typeDisc + "' and SaleDate LIKE '" + Date + "'%");


After a few google searches, I found that after inserting 'and' between the two 'WHERE' clauses, it should return my result.

Any help/examples would be appreciated.
Posted
Updated 6-Oct-11 20:47pm
v2
Comments
Simon Bang Terkildsen 6-Oct-11 18:27pm    
You do not state any problem or question.
Andrew797 6-Oct-11 18:40pm    
sorry, figured it out, missed place single qoute. now when i run this query, i get the following message: "No value given for one or more required parameters." Both the fields in my query exists in the database, and when debugging, both variables have data. Any Idea?

1 solution

I would take a look at the value of the typeDisc (assuming it is a string) and see if there has a single quote in it. If so, you need to escape it by replacing it with two single quotes. On the other hand, if it is an enum variable, then you should use the ToString() function to get its value. Next, look at the Date. If this is a DateTime object, then you will need to use one of the ToString() functions (possibly with the necessary format string) to get the value.

If SalesDate is a datetime datatype, then you may need to do your comparison filter by converting it to a date datatype to ignore the time element:
SQL
"...AND CONVERT([date], SalesDate) = CONVERT([date], '" + Date + "')"


Having said this, it is worth noting that sql queries like this are subject to sql injection attacks. Using parameters is a better method.
 
Share this answer
 
Comments
Bala Selvanayagam 7-Oct-11 4:38am    
well said

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900