Introduction:
Here
I will explain how to write a 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.
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 Email
|
|
|
14 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.
Thank you very much
suresh bhai bole to jhakkasssssssssss suresh bhai....mind bloing.