Click here to Skip to main content
11,496,146 members (367 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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
gvprabu at 4-Mar-13 23:56pm
You have to pass the values like '2008-10-01' and '2008-11-05'
Try the following:
select * from bill_detail where Date >= '" + dt1 + "' AND Date <= '" + dt2 + "'" 
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

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

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

WIKI : SQL Injection[^]

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 5 Mar 2013
Copyright © CodeProject, 1999-2015
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