SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

Fetching records between two date ranges

We can collect records between two date fields of a table by using BETWEEN query. We can use this to get records between two years or between two month . We can combine all this and try for getting records between two date ranges.  

Between two years
We will first start with displaying records between two years. Before that you can read on how to get year part from a date field. Now let us try to get records between year 2004 and 2005. Here is our query

SELECT * FROM `dt_tb` WHERE year( dt2 ) between 2004 and 2005

IN the above query dt2 is our date field and in the result both the years 2004 and 2005 will be included in our records.

Between two month ranges.
Now let us collect the records between two months. Note that if we are using only month in our between command then for any year the range of month we specified will be returned by the query. For example if we ask for records between Feb and Aug months then we will get records of between the month Feb and Aug for all the years. Here is the example.

SELECT * FROM `dt_tb` WHERE month(dt) between '02' and '08'

The above query will return us records of all the months between February and August of any year. We can specify the year also along with the months like this

SELECT * FROM `dt_tb` WHERE month(dt) between '02' and '08' and year(dt) between 2004 and 2005

There are more details on how to get the month part of any date field here.

Now let us move to select a range of records between two dates. Here is the sql for this

SELECT * FROM `dt_tb` WHERE dt BETWEEN '2005-01-01' AND '2005-12-31'


Must Read How to get records between two ranges using between and DATE_SUB function.

Here is the code for SQl dump of the file to create your table for testing.

CREATE TABLE dt_tb (
id int(2) NOT NULL auto_increment,
dt datetime NOT NULL default '0000-00-00 00:00:00',
dt2 date NOT NULL default '0000-00-00',
PRIMARY KEY (id)
) TYPE=MyISAM;


# Dumping data for table `dt_tb`

INSERT INTO dt_tb VALUES (1, '2004-10-26 00:00:00', '2005-01-25');
INSERT INTO dt_tb VALUES (2, '2004-05-05 23:56:25', '2005-06-12');
INSERT INTO dt_tb VALUES (3, '2005-12-08 13:20:10', '2005-06-06');
INSERT INTO dt_tb VALUES (4, '2003-05-26 00:00:00', '2007-12-18');
INSERT INTO dt_tb VALUES (5, '2007-12-18 00:00:00', '2003-08-16');


Discuss this tutorial at forum

List of SQL Tutorials


Further readings
Getting formatted date value from date field in MySQL
Formatting date and time before adding to date field of MySQL
Automatically updating / inserting current date and time value in a DATETIME field
Collecting records between two date ranges from MySQL table fields
Getting date values from MYSQL table in readable format including time
Difference in days between two date fields
Getting the year part from date field
Getting the month part from date field
Getting the day part from date field
Records of last one month from today by using date field
Records of present week days by using dayofweek function
 
Scripts
PHP
JavaScript
HOME
SQL Tutorial List
SQL (Home)
SQL Commands
AVG
Alter Table
Between
Count
Copy Table
Create Table
Delete
Distinct
Group by
Having
Insert
Inner Join
IN
Left join
Limit
Like
MAX
MIN
Order By
OR AND
Rand
Replace
Rename Table
Select Query
Sum
Union
Update
Where
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.