Powered by Blogger.

Saturday, February 15, 2014

Query to get records between two dates in sql server 2008

In this post we will discuss how we can retrieve records between two dates in sql server 2008. Also check out my previous posts on:

- How to populate dataset with data adapter and XML in Asp.Net?

- Delete all stored procedures at once in SQL Server database

- Tutorial on MVVM with WPF

Retrieve record from last 7 days:

Suppose you have an employee table and JoiningDate is a column and you want to retrieve who are the employees joined in last 7 days, then you can query like below:

select * from Employees WHERE JoiningDate BETWEEN GetDate()-7 AND GetDate()

Retrieve record between particular dates:

Below is the query to retrieve records between particular dates.

BETWEEN CONVERT(VARCHAR,'30-08-2013',105) AND CONVERT(VARCHAR,'05-09-2013',105)

Retrieve record for particular month and year:

Below is the query that will retrieve records for particular month and year.

The below query will retrieve record for the month of july and year of 2013:

SELECT * FROM Employees
   WHERE MONTH(JoiningDate) = 7 AND YEAR(JoiningDate) = 2013