Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I am developing an desktop application in c# and sql server.

My problem is i have written sql query for displaying data ranging between 2 dates.

My query is
select tblSalesTransaction.date,tblSalesTransaction.inv from tblSalesTransaction where tblSalesTransaction.date between '" + dtpFrom.Value + "' and '" + dtpTo.Value + "'


If i select from date 01/02/2012 and to date 05/02/2012 the data should start from 01/02/2012-05/02/2012 but i am getting data from 02/02/2012-05/02/2012.

Please help me .............
Posted
Updated 31-Jan-12 23:47pm
v2
Comments
Sridhar Patnayak 1-Feb-12 5:49am    
Check your table records.
yogesh89 1-Feb-12 5:51am    
the records start from 01/02/2011..
Rajesh Anuhya 1-Feb-12 5:52am    
Code tags added
yogesh89 1-Feb-12 5:55am    
not understood this comment.......

1 solution

SQL
--If you use SQL 2008, then try the following:
select tblSalesTransaction.date,tblSalesTransaction.inv from tblSalesTransaction where CAST(tblSalesTransaction.date AS Date)  between CAST('" + dtpFrom.Value + "' AS DAte) and CAST('" + dtpTo.Value + "', as Date)"

SQL
--For older versions, try the following:
select tblSalesTransaction.date,tblSalesTransaction.inv from tblSalesTransaction where dateadd(dd,0, datediff(dd,0, tblSalesTransaction)) between dateadd(dd,0, datediff(dd,0, " + dtpFrom.Value + " )) AND dateadd(dd,0, datediff(dd,0, " + dtpTo.Value + " )) "
 
Share this answer
 
Comments
yogesh89 1-Feb-12 6:21am    
ok i will try it out....
yogesh89 1-Feb-12 6:32am    
it isworking fine....thanks for answer.........

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