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.