Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am having problem with sql query. The query is as below


SQL
SELECT Products.ProductName, Products.origionalPrice, OrderDetails.Quantity,OrderDetails.UnitCost FROM Products LEFT JOIN OrderDetails ON Products.ProductID=OrderDetails.ProductID where OrderDetails.PurchaseDate between '1/4/2012' and '1/4/2012' 


however this fallowing query works fine and shows results. but with where clause and between command in sql query gives no results the table is empty i have a date in purchasdate colum of OrdersDetails table which is 1/4/2012. y data is not displayed in table

SQL
SELECT Products.ProductName, Products.origionalPrice, OrderDetails.Quantity,OrderDetails.UnitCost FROM Products LEFT JOIN OrderDetails ON Products.ProductID=OrderDetails.ProductID



C#
public void bind()
    {
               string query = "SELECT Products.ProductName, Products.origionalPrice, OrderDetails.Quantity,OrderDetails.UnitCost FROM Products LEFT JOIN OrderDetails ON Products.ProductID=OrderDetails.ProductID where OrderDetails.PurchaseDate Between '" + DropDownList1.Text + "' and '" + DropDownList2.Text + "'";
        DataSet ds = obj.fillgrid(query);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
Posted
Updated 4-Jan-12 3:33am
v4
Comments
Prasad_Kulkarni 4-Jan-12 9:20am    
how you have executed your query using where clause can u post that code??
codegeekalpha 4-Jan-12 9:33am    
i am testing this query in sqlserver. in code i am fetching date in dropdownlist from database and i am using where clause with dropdown list, question updated
devbtl 4-Jan-12 10:21am    
check date format in dropdown.
Corporal Agarn 4-Jan-12 11:25am    
do you see the data it you use '1/5/2012' as the second date? There may be a problem with the default date format mm/dd versus dd/mm.
codegeekalpha 4-Jan-12 11:37am    
no problem with that...

First of all, don't use literals, always use SqlParameter[^]. This will help you with conversion problems and protect against SQL injections.

If the problem is the time portion, you can either define the column in the database as date if you don't need the time potion at all or if you need it, then one way is to eliminate it in your query. For the latter the querty could look something like:
SQL
SELECT Products.ProductName, Products.origionalPrice, OrderDetails.Quantity,OrderDetails.UnitCost 
FROM Products LEFT JOIN OrderDetails 
ON Products.ProductID=OrderDetails.ProductID 
WHERE  CAST(OrderDetails.PurchaseDate AS date) BETWEEN '1/4/2012' and '1/4/2012' 

Or actually when parameters are correctly used, it would be:
SQL
SELECT Products.ProductName, Products.origionalPrice, OrderDetails.Quantity,OrderDetails.UnitCost 
FROM Products LEFT JOIN OrderDetails 
ON Products.ProductID=OrderDetails.ProductID 
WHERE  CAST(OrderDetails.PurchaseDate AS date) BETWEEN @startDate and @endDate 
 
Share this answer
 
Comments
Amir Mahfoozi 10-Jan-12 0:38am    
+5
Wendelius 10-Jan-12 0:43am    
Thanks :)
Amir Mahfoozi 10-Jan-12 4:52am    
Salutes and congratulations on becoming MVP 2012 ;)
Wendelius 10-Jan-12 14:23pm    
Thank you Amir :)
It is converting '1/4/2012' to '1/4/2012 00:00'
So you don't have any between '1/4/2012 00:00' and '1/4/2012 00:00'

Change your second date to '1/5/2012'
 
Share this answer
 
I see a couple of problems:
1) It is always a good idea to be completely unambiguous with datetimes

One way is to use the standard SQLS unseparated format 'yyyymmdd hh:mm:ss.mmm' to define your dates

e.g.

SELECT Products.ProductName, Products.origionalPrice, OrderDetails.Quantity,OrderDetails.UnitCost FROM Products LEFT JOIN OrderDetails ON Products.ProductID=OrderDetails.ProductID where OrderDetails.PurchaseDate between '20120104' and '20120104 23:59:59.999'

(or between '20120104 00:00:00.000' and '20120104 23:59:59.999' if you want to make it obvious that the first date is midnight of 3rd Jan 2012)

If the time component isn't significant then there is always the option of ignoring it...

where cast(datediff(day,0,OrderDetails.PurchaseDate) as datetime) between cast(datediff(day,0,'20120104') as datetime) and cast(datediff(day,0,'20120104') as datetime) will return all rows with a date sometime on the 4th

2) The query itself is suspect, the where clause is not executed as part of the left join. Therefore if you have no order details with a matching purchase date then you will also not see the product details.

If you wish to see the product details irrespective of whether the order details appear then the query needs to change to something like this (includes all rows with a date on the 4th or 5th Jan 2012):

SELECT Products.ProductName, Products.origionalPrice, OrderDetails.Quantity,OrderDetails.UnitCost FROM Products LEFT JOIN OrderDetails ON Products.ProductID=OrderDetails.ProductID and cast(datediff(day,0,OrderDetails.PurchaseDate) as datetime) between cast(datediff(day,0,'20120104') as datetime) and cast(datediff(day,0,'20120105') as datetime)
 
Share this answer
 
what is the datatype of your field "PurchaseDate "?

i think it is DateTime, so the query below should be working:

SELECT Products.ProductName, Products.origionalPrice, OrderDetails.Quantity,OrderDetails.UnitCost FROM Products LEFT JOIN OrderDetails ON Products.ProductID=OrderDetails.ProductID where OrderDetails.PurchaseDate between '1/4/2012 00:00:00' and '1/4/2012 23:59:59'
 
Share this answer
 
Try this
The date we are passing is in 'dd-MM-yyyy' format
SQL
SELECT Products.ProductName, Products.origionalPrice, OrderDetails.Quantity,OrderDetails.UnitCost 
FROM Products LEFT JOIN OrderDetails ON Products.ProductID=OrderDetails.ProductID 
where OrderDetails.PurchaseDate BETWEEN CONVERT(DATETIME,'01-04-2011',105) AND CONVERT(DATETIME,'01-04-2012',105)
 
Share this answer
 
v2
Comments
Corporal Agarn 4-Jan-12 14:01pm    
where OrderDetails.PurchaseDate BETWEEN CAST(DATETIME,'01-04-2011',105) AND CONVERT(DATETIME,'01-04-2012',105)+ 1

or somehow make the date midnight (see change by USABebopKid)
Sorry, Try this. It will work

SQL
SELECT Products.ProductName, Products.origionalPrice, OrderDetails.Quantity,OrderDetails.UnitCost 
FROM Products LEFT JOIN OrderDetails ON Products.ProductID=OrderDetails.ProductID 
where CONVERT(DATETIME,CONVERT(VARCHAR,OrderDetails.PurchaseDate,105),105) BETWEEN CONVERT(DATETIME,'01-04-2012',105) AND CONVERT(DATETIME,'01-04-2012',105)
 
Share this answer
 
v2

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