Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET SQL-Server-2008
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
SqlDataAdapter da1 = new SqlDataAdapter("select * from bill_detail where Date BETWEEN '" + dt1 + "' AND '" + dt2 + "'", con);
Posted 4-Mar-13 18:53pm
Edited 4-Mar-13 18:58pm
gvprabu7.8K
v2
Comments
gvprabu at 4-Mar-13 23:56pm
   
You have to pass the values like '2008-10-01' and '2008-11-05'
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try the following:
select * from bill_detail where Date >= '" + dt1 + "' AND Date <= '" + dt2 + "'" 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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

 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[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 499
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 5 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100