Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server - Query to Get Data between Two Dates in SQL Server

Aug 11, 2012
Introduction:

Here I will explain how to write a SQL query to get data between two dates in SQL Server.

Description:

In previous articles I explained many articles relating to SQL Server. Now I will explain how to write a query to get data between two dates in SQL Server. In database I have one table UserInformation that contain data like as shown below

Now I want to get the data from UserInformation table which are registered in between 07/08/2012 and 10/08/2012 for that we need to write a query like as shown below

Syntax Query to get data between two dates

If we want to get the records between two dates we need to write as shown below


SELECT Column1,Column2,Column3 FROM TABLENAME WHERE Column3 BETWEEN GetDate()-1 AND GetDate()
Query to get records between 07/08/2012 and 10/08/2012

If you want get records between these two days we need to write a query like as shown below


SELECT * FROM #temp WHERE CONVERT(VARCHAR(10),CrDate,103) BETWEEN CONVERT(VARCHAR(10),'07/08/2012',103) AND CONVERT(VARCHAR(10),'10/08/2012',103)
If you observe above query I used CONVERT statement for dates because here I am comparing with only dates not with datetime that’s why I used conversion format to match the dates.

If you want to know complete conversion formats check this post Get only date from datetime in sql server.  

Output


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

23 comments :

SAGAR said...

Brilliant.....big fan of yew :)
-Sagar

Admin said...

very useful sir.....!

my doubt is...if i want to a sql query to list-out the persons whose date of birth is today to wish birthday...thanks in advance.

Anonymous said...

Anonymous said...

if i search record between 01/09/2012 to 03/09/2012, then they show record between 03/01/2011 to 03/09/2012...

query:- "SELECT ID_No,APPLI,NM,PlanType,TAMT,SYS_DATE,ScanJPG,ScanDate,ScanUser from MoneyRectD WHERE CONVERT(VARCHAR(10),SYS_DATE,103) BETWEEN CONVERT(VARCHAR(10),'" +dateTimePicker1.Value + "',103) AND CONVERT(VARCHAR(10),'" + dateTimePicker2.Value + "',103)"

Why...?

thx & regard

email Id:- manmohan_jha@sify.com

Suresh Dasari said...

Please check your datetimepicker values whether that values correct or not and check in your database whether the records exists or not for those particular dates.

Anonymous said...

masktextbox ka use krne pe bhi yehi problem aa rah hai...

ye query sirf 1 day record show ke liye sahi hai...

Help me...

Nandhan said...

cmd = new OleDbCommand("select * from CheckOut_Details where CheckOut_CheckOutDate between '" + dtpFromCollection.Value.ToString("dd/MMM/yyyy") + "' and '" + dtpToCollection.Value.ToString("dd/MMM/yyyy") + "' ", con);
here is my query which is not selecting all the recrods from database
Pls help me out!!!
Thanks in Advance

Unknown said...

How to generate report between two dates in asp.net by entering date into two textboxes using ur query plz??

Anonymous said...

this is very useful ..thank u....

Unknown said...

i want to fetch data between two different months like
Select * from BillMaster where flgIsDeleted=0 AND convert(char(20),dtInsDate,105) BETWEEN convert(char(20),'04-04-2013',105) AND convert(char(20),'5-04-2013',105)

but this not working ??
thanks,,

Kamal Pandey said...

How to use checkbox in datagrid in C# window Application and update or delete a data in datbase.

Kamal Pandey said...

How to retrive data between two date in C# window application in Access Database.

Unknown said...

suresh bhai bole to jhakkasssssssssss suresh bhai....mind bloing.

Anonymous said...

it works for me and thank u very much .....

Anonymous said...

select * from SubTaAssign WHERE CONVERT(varchar,creda,103) BETWEEN CONVERT(varchar,'22/08/2013',103) AND CONVERT(varchar,'23/08/2013',103)
when i cheng date like it is not working

Unknown said...

Hello Experts....
Am new to here.. i hav a doubt why we include 103

Unknown said...

how to write this code in aspx page, i want to implement it from front end, can u help me???

Anonymous said...

this query is not working if my Date is from 2/01/2014 to 2/02/2013 which is from two diff months pls help ......

Unknown said...

You can use this query to filter data between two dates:-
DateTime dt1 = this.dateTimePicker1.Value.Date;
DateTime dt2 = this.dateTimePicker2.Value.Date.AddMinutes(1440);
string query = "SELECT * FROM student WHERE sdate BETWEEN '"+dt1+"' AND '"+dt2+"'";

Anonymous said...

what datatype have you used for storing date in database?

Anonymous said...

i want to display records from todays date to upcoming dates which query should i fire??

Unknown said...

"SELECT count(*) from Category WHERE Rank like 'Master' And Date_Time Between #"+DateTimePicker2.Text+ "# And " + DateTimePicker1.Text+ ""

is this right code

Unknown said...

an using msaccess database, want to fetch data between from date to todate in c# coding;
please help me sir. how am done it

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.