Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hey Guys,
I Want to apply Between Operator for to find data between Two dates and I write this code but it find only through two day. as like suppose I write 1-10-2008 and 5-11-2008 then it find data between 1 and 5 not from month and year. How to do find data from whole Date Please Reply Me...!!!

I use data type Varchar(Max) and then after replace varchar I use Datetime But It Can't Find...Please Reply Me...!!!

My Code is Below
VB
SqlDataAdapter da1 = new SqlDataAdapter("select * from bill_detail where Date BETWEEN '" + dt1 + "' AND '" + dt2 + "'", con);
Posted
Updated 4-Mar-13 17:58pm
v2
Comments
gvprabu 4-Mar-13 23:56pm    
You have to pass the values like '2008-10-01' and '2008-11-05'

Try the following:
C#
select * from bill_detail where Date >= '" + dt1 + "' AND Date <= '" + dt2 + "'" 
 
Share this answer
 
 
Share this answer
 
Try to pass the appropriate date. By default the date format will be a long string which includes date, time, day..

Use the syntax to format the date:

Let's say you are taking date from textbox then the syntax should be this way.

string strDate = txtDate.ToString("dd/MM/yyyy");

or you can use any date format based on your requirement

MM/dd/yyyy
yyyy/MM/dd
etc..
 
Share this answer
 
Assuming that your dates are in the format dd/mm/yyyy i.e. 01/03/2013 you can do the following


SQL
SELECT *
FROM bill_detail
WHERE Date BETWEEN cast('" + dt1 + "' as DATETIME) AND CAST('" + dt2 + "' as DATETIME)


but also please look into parameterised queries as this will help you prevent SQL Injection

http://www.dotnetperls.com/sqlparameter[^]

WIKI : SQL Injection[^]
 
Share this 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