Introduction:
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. |
|||
|
|||
23 comments :
Brilliant.....big fan of yew :)
-Sagar
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.
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
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.
masktextbox ka use krne pe bhi yehi problem aa rah hai...
ye query sirf 1 day record show ke liye sahi hai...
Help me...
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
How to generate report between two dates in asp.net by entering date into two textboxes using ur query plz??
this is very useful ..thank u....
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,,
How to use checkbox in datagrid in C# window Application and update or delete a data in datbase.
How to retrive data between two date in C# window application in Access Database.
suresh bhai bole to jhakkasssssssssss suresh bhai....mind bloing.
it works for me and thank u very much .....
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
Hello Experts....
Am new to here.. i hav a doubt why we include 103
how to write this code in aspx page, i want to implement it from front end, can u help me???
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 ......
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+"'";
what datatype have you used for storing date in database?
i want to display records from todays date to upcoming dates which query should i fire??
"SELECT count(*) from Category WHERE Rank like 'Master' And Date_Time Between #"+DateTimePicker2.Text+ "# And " + DateTimePicker1.Text+ ""
is this right code
an using msaccess database, want to fetch data between from date to todate in c# coding;
please help me sir. how am done it
Note: Only a member of this blog may post a comment.